jsmodern
Version:
An extension to existing JavaScript, influenced by other great languages such as Rust, Dart, Java, Golang, etc.
42 lines • 1.2 kB
JavaScript
export const split = {
label: 'split',
fn: function arraySplit(predicate) {
const ctx = this;
if ('function' !== typeof (predicate)) {
throw new TypeError(`Expect 'predicate' to be a function`);
}
const len = ctx.length;
if (!len)
return [[]];
const set = [];
let subset = [];
let count = 2;
for (let i = 0; i < len; i += 1) {
const val = ctx[i];
const shouldSplit = predicate(val);
if (shouldSplit) {
count -= 1;
if (subset.length > 0) {
set.push(subset);
subset = [];
}
if (!i)
set.push([]);
if (len - 1 === i)
set.push([]);
if (!count) {
set.push([]);
count = 2;
}
continue;
}
if (count < 2)
count = 2;
subset.push(val);
}
if (subset.length > 0)
set.push(subset);
return set;
},
};
//# sourceMappingURL=split.js.map