UNPKG

diginext-utils

Version:
32 lines 870 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.chunk = chunk; /** * Splits an array into chunks of a specified size. * * @template T - The type of elements in the array * @param array - The array to chunk * @param size - The size of each chunk (must be positive) * @returns An array of chunks * * @example * ```ts * chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]] * chunk(['a', 'b', 'c', 'd'], 3); // [['a', 'b', 'c'], ['d']] * chunk([1, 2, 3], 1); // [[1], [2], [3]] * ``` */ function chunk(array, size) { if (!Array.isArray(array) || array.length === 0) { return []; } if (size <= 0) { return []; } const result = []; for (let i = 0; i < array.length; i += size) { result.push(array.slice(i, i + size)); } return result; } //# sourceMappingURL=chunk.js.map