@validatem/core
Version:
The last validation library you'll ever need.
23 lines (17 loc) • 655 B
JavaScript
;
// 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;
};