es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
16 lines (14 loc) • 465 B
JavaScript
function chunk(arr, size) {
if (!Number.isInteger(size) || size <= 0) {
throw new Error('Size must be an integer greater than zero.');
}
const chunkLength = Math.ceil(arr.length / size);
const result = Array(chunkLength);
for (let index = 0; index < chunkLength; index++) {
const start = index * size;
const end = start + size;
result[index] = arr.slice(start, end);
}
return result;
}
export { chunk };