jsmodern
Version:
An extension to existing JavaScript, influenced by other great languages such as Rust, Dart, Java, Golang, etc.
30 lines • 864 B
JavaScript
export const chunks = {
label: 'chunks',
fn: function arrayChunks(chunkSize) {
const ctx = this;
const len = ctx.length;
const chunkSizeInt = Number(chunkSize);
if ('number' !== typeof (chunkSize) || chunkSizeInt < 1) {
throw new TypeError(`Expect 'chunkSize' to be a number that is at least 1`);
}
if (!len)
return [];
if (1 === len || len === chunkSizeInt)
return [ctx];
const set = [];
let subset = [];
let i = chunkSizeInt;
for (const n of ctx) {
if (!i) {
i = chunkSizeInt;
set.push(subset);
subset = [];
}
subset.push(n);
i -= 1;
}
set.push(subset);
return set;
},
};
//# sourceMappingURL=chunks.js.map