UNPKG

typedash

Version:

modern, type-safe collection of utility functions

25 lines (24 loc) 694 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 export { chunkArray as n, chunk as t }; //# sourceMappingURL=chunk-B70fG6Ov.js.map