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.
18 lines (17 loc) • 444 B
JavaScript
// src/index.ts
var chunkArray = (array, maxLength) => {
if (maxLength <= 0) {
throw new Error("maxLength must be greater than 0");
}
const length = array.length;
if (length === 0) return [];
const chunks = Math.ceil(length / maxLength);
const result = new Array(chunks);
for (let i = 0, j = 0; i < length; i += maxLength, j++) {
result[j] = array.slice(i, i + maxLength);
}
return result;
};
export {
chunkArray
};