isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
30 lines (29 loc) • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.newArray = newArray;
const deepCopy_1 = require("./deepCopy");
const utils_1 = require("./utils");
/**
* Initializes an array with all of the elements containing the specified default value.
*
* The provided default value will be copied with the `deepCopy` function before adding it to the
* new array. Thus, you can initialize an array of arrays, or an array of maps, and so on. (If the
* `deepCopy` function was not used, then all of the array elements would just be references to the
* same underlying data structure.)
*
* For example:
*
* ```ts
* const arrayWithZeroes = newArray(0, 10); // Has 10 elements of 0.
* const arrayWithArrays = newArray([0], 20); // Has 20 elements of an array with a 0 in it.
* ```
*/
// eslint-disable-next-line complete/no-mutable-return
function newArray(defaultValue, size) {
const array = [];
(0, utils_1.repeat)(size, () => {
const copy = (0, deepCopy_1.deepCopy)(defaultValue);
array.push(copy);
});
return array;
}