You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
655 B
JavaScript

"use strict";
// FIXME: Move this into its own package
module.exports = function splitArray(array, indexes) {
let segments = [];
let lastIndex = 0;
for (let index of indexes) {
if (index < array.length) {
segments.push(array.slice(lastIndex, index));
lastIndex = index;
} else {
throw new Error(`Attempted to split at index ${index}, but array only has ${array.length} items`);
}
}
// Final segment, from last specified index to end of array; this is because the indexes only define the *split points*, and so the total amount of segments is always one more than that.
segments.push(array.slice(lastIndex));
return segments;
};