UNPKG

@blakek/array-split

Version:

💔 Split and chunk arrays, strings, and more

48 lines (41 loc) • 1.38 kB
(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports"], factory); } else if (typeof exports !== "undefined") { factory(exports); } else { var mod = { exports: {} }; factory(mod.exports); global.arraySplit = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) { "use strict"; _exports.__esModule = true; _exports.chunk = chunk; _exports.splitAtIndex = splitAtIndex; _exports.splitAtIndices = splitAtIndices; _exports["default"] = void 0; function chunk(chunkSize, array) { if (array.length <= chunkSize) return [array]; return [array.slice(0, chunkSize)].concat(chunk(chunkSize, array.slice(chunkSize))); } function splitAtIndex(index, array) { return [array.slice(0, index), array.slice(index)]; } function splitAtIndices(_ref, array) { var index = _ref[0], nextIndex = _ref[1], indices = _ref.slice(2); if (index === undefined) return [array]; var nextIndices = [nextIndex ? nextIndex - index : nextIndex].concat(indices); return [array.slice(0, index)].concat(splitAtIndices(nextIndices, array.slice(index))); } var index = { chunk: chunk, splitAtIndex: splitAtIndex }; var _default = index; _exports["default"] = _default; });