'use strict'; var set = require('./set-0f209abb.cjs'); /** * Utility module to work with Arrays. * * @module array */ /** * Return the last element of an array. The element must exist * * @template L * @param {ArrayLike} arr * @return {L} */ const last = arr => arr[arr.length - 1]; /** * @template C * @return {Array} */ const create = () => /** @type {Array} */ ([]); /** * @template D * @param {Array} a * @return {Array} */ const copy = a => /** @type {Array} */ (a.slice()); /** * Append elements from src to dest * * @template M * @param {Array} dest * @param {Array} src */ const appendTo = (dest, src) => { for (let i = 0; i < src.length; i++) { dest.push(src[i]); } }; /** * Transforms something array-like to an actual Array. * * @function * @template T * @param {ArrayLike|Iterable} arraylike * @return {T} */ const from = Array.from; /** * True iff condition holds on every element in the Array. * * @function * @template ITEM * @template {ArrayLike} ARR * * @param {ARR} arr * @param {function(ITEM, number, ARR):boolean} f * @return {boolean} */ const every = (arr, f) => { for (let i = 0; i < arr.length; i++) { if (!f(arr[i], i, arr)) { return false } } return true }; /** * True iff condition holds on some element in the Array. * * @function * @template S * @template {ArrayLike} ARR * @param {ARR} arr * @param {function(S, number, ARR):boolean} f * @return {boolean} */ const some = (arr, f) => { for (let i = 0; i < arr.length; i++) { if (f(arr[i], i, arr)) { return true } } return false }; /** * @template ELEM * * @param {ArrayLike} a * @param {ArrayLike} b * @return {boolean} */ const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index]); /** * @template ELEM * @param {Array>} arr * @return {Array} */ const flatten = arr => fold(arr, /** @type {Array} */ ([]), (acc, val) => acc.concat(val)); /** * @template T * @param {number} len * @param {function(number, Array):T} f * @return {Array} */ const unfold = (len, f) => { const array = new Array(len); for (let i = 0; i < len; i++) { array[i] = f(i, array); } return array }; /** * @template T * @template RESULT * @param {Array} arr * @param {RESULT} seed * @param {function(RESULT, T, number):RESULT} folder */ const fold = (arr, seed, folder) => arr.reduce(folder, seed); const isArray = Array.isArray; /** * @template T * @param {Array} arr * @return {Array} */ const unique = arr => from(set.from(arr)); /** * @template T * @template M * @param {ArrayLike} arr * @param {function(T):M} mapper * @return {Array} */ const uniqueBy = (arr, mapper) => { /** * @type {Set} */ const happened = set.create(); /** * @type {Array} */ const result = []; for (let i = 0; i < arr.length; i++) { const el = arr[i]; const mapped = mapper(el); if (!happened.has(mapped)) { happened.add(mapped); result.push(el); } } return result }; /** * @template {ArrayLike} ARR * @template {function(ARR extends ArrayLike ? T : never, number, ARR):any} MAPPER * @param {ARR} arr * @param {MAPPER} mapper * @return {Array} */ const map = (arr, mapper) => { /** * @type {Array} */ const res = Array(arr.length); for (let i = 0; i < arr.length; i++) { res[i] = mapper(/** @type {any} */ (arr[i]), i, /** @type {any} */ (arr)); } return /** @type {any} */ (res) }; var array = /*#__PURE__*/Object.freeze({ __proto__: null, last: last, create: create, copy: copy, appendTo: appendTo, from: from, every: every, some: some, equalFlat: equalFlat, flatten: flatten, unfold: unfold, fold: fold, isArray: isArray, unique: unique, uniqueBy: uniqueBy, map: map }); exports.appendTo = appendTo; exports.array = array; exports.copy = copy; exports.create = create; exports.equalFlat = equalFlat; exports.every = every; exports.flatten = flatten; exports.fold = fold; exports.from = from; exports.isArray = isArray; exports.last = last; exports.map = map; exports.some = some; exports.unfold = unfold; exports.unique = unique; exports.uniqueBy = uniqueBy; //# sourceMappingURL=array-704ca50e.cjs.map