just-range
Version:
Generate a range array for numbers
37 lines (33 loc) • 926 B
JavaScript
var arrayRange = range;
/*
range(0, 5); // [0, 1, 2, 3, 4]
range(5); // [0, 1, 2, 3, 4]
range(-5); // [0, -1, -2, -3, -4]
range(0, 20, 5) // [0, 5, 10, 15]
range(0, -20, -5) // [0, -5, -10, -15]
*/
function range(start, stop, step) {
if (start != null && typeof start != 'number') {
throw new Error('start must be a number or null');
}
if (stop != null && typeof stop != 'number') {
throw new Error('stop must be a number or null');
}
if (step != null && typeof step != 'number') {
throw new Error('step must be a number or null');
}
if (stop == null) {
stop = start || 0;
start = 0;
}
if (step == null) {
step = stop > start ? 1 : -1;
}
var toReturn = [];
var increasing = start < stop; //← here’s the change
for (; increasing ? start < stop : start > stop; start += step) {
toReturn.push(start);
}
return toReturn;
}
export {arrayRange as default};