UNPKG

@technobuddha/library

Version:
24 lines (23 loc) 1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.create2DArray = void 0; var isFunction_1 = __importDefault(require("lodash/isFunction")); /** * Create a two dimensional array with all elements initialized * * @remark Array is accessed by array[w][h] * * @param width Width of the array * @param height Height of the array * @param fill value to fill the array, or a function returning the fill value for each element */ function create2DArray(width, height, fill) { if (isFunction_1.default(fill)) return Array.from(new Array(width), function (_1, x) { return Array.from(new Array(height), function (_2, y) { return fill(x, y); }); }); return Array.from(new Array(width), function () { return new Array(height).fill(fill); }); } exports.create2DArray = create2DArray; exports.default = create2DArray;