@blakek/array-split
Version:
💔 Split and chunk arrays, strings, and more
20 lines (18 loc) • 650 B
JavaScript
function chunk(chunkSize, array) {
if (array.length <= chunkSize) return [array];
return [array.slice(0, chunkSize), ...chunk(chunkSize, array.slice(chunkSize))];
}
function splitAtIndex(index, array) {
return [array.slice(0, index), array.slice(index)];
}
function splitAtIndices([index, nextIndex, ...indices], array) {
if (index === undefined) return [array];
const nextIndices = [nextIndex ? nextIndex - index : nextIndex, ...indices];
return [array.slice(0, index), ...splitAtIndices(nextIndices, array.slice(index))];
}
var index = {
chunk,
splitAtIndex
};
export default index;
export { chunk, splitAtIndex, splitAtIndices };