locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
36 lines (35 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.slice_when = slice_when;
function slice_when(arr, boundary) {
// discuss at: https://locutus.io/ruby/Array/slice_when/
// parity verified: Ruby 3.3
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Starts a new slice whenever boundary(previous, current) returns true.
// note 2: Mirrors Ruby's Enumerable#slice_when behavior on arrays.
// example 1: slice_when([1, 2, 4, 5, 7, 8], (prev, curr) => curr - prev !== 1)
// returns 1: [[1, 2], [4, 5], [7, 8]]
// example 2: slice_when(['a', 'ab', 'abc', 'd', 'de'], (prev, curr) => curr.length <= prev.length)
// returns 2: [['a', 'ab', 'abc'], ['d', 'de']]
// example 3: slice_when([], () => true)
// returns 3: []
if (!Array.isArray(arr) || arr.length === 0 || typeof boundary !== 'function') {
return [];
}
const source = arr;
const out = [[source[0]]];
for (let i = 1; i < source.length; i++) {
const previous = source[i - 1];
const current = source[i];
const shouldSlice = Boolean(boundary(previous, current, i, source));
if (shouldSlice) {
out.push([current]);
continue;
}
const last = out.at(-1);
if (last) {
last.push(current);
}
}
return out;
}