just-chunk-array
Version:
A utility function that splits an array into subarrays (chunks) of a specified maximum length. [444B](https://www.npmjs.com/package/just-chunk-array?activeTab=code) total js-code size.
19 lines (17 loc) • 714 B
TypeScript
/**
* Splits an array into chunks of a specified maximum length.
*
* @template T - The type of elements in the input array.
* @param {T[]} array - The array to be split.
* @param {number} maxLength - The maximum number of elements per chunk (must be greater than 0).
* @returns {T[][]} An array of chunks, where each chunk is a subarray of the original array.
* @throws {Error} Throws an error if maxLength is less than or equal to 0.
*
* @example
* // Example usage:
* const numbers = [1, 2, 3, 4, 5];
* const chunked = chunkArray(numbers, 2);
* console.log(chunked); // Output: [[1, 2], [3, 4], [5]]
*/
declare const chunkArray: <T>(array: T[], maxLength: number) => T[][];
export { chunkArray };