Created
August 22, 2016 14:14
-
-
Save ughitsaaron/6fc44dc7481eaa9559c2423bc5333942 to your computer and use it in GitHub Desktop.
JavaScript Ranges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A function to generate array ranges (zero-indexed) | |
| * @param {number} from | |
| * @param {number} to | |
| */ | |
| export function RangeArray(from, to) { | |
| var r = []; | |
| if (typeof from !== 'number' && typeof to !== 'number') { | |
| throw new Error('RangeArray expects a number'); | |
| } | |
| r = [...Array(to || from).keys()]; | |
| return to ? r.slice(from) : r; | |
| } | |
| // new RangeArray(5) => [0,1,2,3,4] | |
| // new RangeArray(5, 10) => [5,6,7,8,9,10,11,12,13,14] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment