rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
35 lines • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrayMapRange = void 0;
const _debug_js_1 = require("../../debug/_debug.js");
/**
* @public
* Like {@link arrayMap} with integer range as input.
*
* @param from - The value to start from (inclusive).
* @param to - The value to finish with (inclusive).
* @param callback - Called for each value in the range.
*
* @returns An array of results from the callback.
*
* @remarks
* Where `from` and `to` are equal a length 1 array is returned, NaN input is not supported.
*
* See {@link arrayMapRange}.
*/
function arrayMapRange(from, to, callback) {
_BUILD.DEBUG && _debug_js_1._Debug.runBlock(() => {
_debug_js_1._Debug.assert(!isNaN(from) && !isNaN(to), "NaN range not supported");
});
const range = to - from;
const length = Math.abs(range) + 1;
const increment = Math.sign(range);
const array = new Array(length);
for (let i = 0; i < length; ++i) {
array[i] = callback(from, i);
from += increment;
}
return array;
}
exports.arrayMapRange = arrayMapRange;
//# sourceMappingURL=array-map-range.js.map