@extra-array/cut-at
Version:
Breaks array at given indices.
16 lines (15 loc) • 338 B
JavaScript
;
function index(x, i = 0) {
return i < 0 ? Math.max(x.length + i, 0) : Math.min(i, x.length);
}
function cutAt(x, is) {
var a = [], j = 0;
for (var i of is) {
i = Math.max(j, index(x, i));
a.push(x.slice(j, i));
j = i;
}
a.push(x.slice(i));
return a;
}
module.exports = cutAt;