UNPKG

@blakek/array-split

Version:

💔 Split and chunk arrays, strings, and more

26 lines (22 loc) • 785 B
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); 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 }; exports.chunk = chunk; exports.default = index; exports.splitAtIndex = splitAtIndex; exports.splitAtIndices = splitAtIndices;