UNPKG

typedash

Version:

modern, type-safe collection of utility functions

37 lines (35 loc) 884 B
//#region src/functions/chunk/chunk.ts /** * Splits an array into chunks of the given size. * @param array The array to split. * @param size The maximum size of each chunk. * @returns An array of arrays where each sub-array has at most `size` elements. * @example * ```ts * chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]] * ``` */ function chunk(array, size) { if (array == null) return []; const chunks = []; for (let index = 0; index < array.length; index += size) chunks.push(array.slice(index, index + size)); return chunks; } /** * Alias for {@link chunk}. */ const chunkArray = chunk; //#endregion Object.defineProperty(exports, 'chunk', { enumerable: true, get: function () { return chunk; } }); Object.defineProperty(exports, 'chunkArray', { enumerable: true, get: function () { return chunkArray; } }); //# sourceMappingURL=chunk-DiKDW-kV.cjs.map