@arrows/array
Version:
Functional tools for JS arrays
40 lines (39 loc) • 931 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.range_ = void 0;
/**
* Creates an array of numbers in a provided range - ascending or descending.
*
* @param from Starting number (included)
* @param to Ending number (excluded)
* @param step Step (must be greater than zero)
* @returns Range array
*/
const range_ = (from, to, step = 1) => {
if (from === to) {
return [];
}
if (step <= 0) {
throw new Error("Step must be greater than zero.");
}
const arr = new Array(Math.floor(Math.abs(to - from) / step));
let i = from;
let j = 0;
if (from > to) {
while (i > to) {
arr[j] = i;
i -= step;
j++;
}
}
else {
while (i < to) {
arr[j] = i;
i += step;
j++;
}
}
return arr;
};
exports.range_ = range_;
exports.default = range_;
;