UNPKG

super-utils-plus

Version:

A superior alternative to Lodash with improved performance, TypeScript support, and developer experience

33 lines (32 loc) 867 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.chunk = chunk; /** * Creates an array of elements split into groups the length of size. * If array can't be split evenly, the final chunk will be the remaining elements. * * @param array - The array to process * @param size - The length of each chunk * @returns The new array of chunks * * @example * ```ts * chunk([1, 2, 3, 4], 2); * // => [[1, 2], [3, 4]] * * chunk([1, 2, 3, 4, 5], 2); * // => [[1, 2], [3, 4], [5]] * ``` */ function chunk(array, size = 1) { if (!array || !array.length) { return []; } // Ensure size is at least 1 const chunkSize = Math.max(Math.floor(size), 1); const result = []; for (let i = 0; i < array.length; i += chunkSize) { result.push(array.slice(i, i + chunkSize)); } return result; }