Skip to content

Instantly share code, notes, and snippets.

@ughitsaaron
Created August 22, 2016 14:14
Show Gist options
  • Select an option

  • Save ughitsaaron/6fc44dc7481eaa9559c2423bc5333942 to your computer and use it in GitHub Desktop.

Select an option

Save ughitsaaron/6fc44dc7481eaa9559c2423bc5333942 to your computer and use it in GitHub Desktop.
JavaScript Ranges
/**
* 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