mirror of
https://github.com/swc-project/swc.git
synced 2024-12-20 20:22:26 +03:00
35 lines
958 B
TypeScript
35 lines
958 B
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/splitWhenever.js
|
||
|
|
||
|
|
||
|
import _curryN from './internal/_curryN.js';
|
||
|
/**
|
||
|
* Splits an array into slices on every occurrence of a value.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.26.1
|
||
|
* @category List
|
||
|
* @sig (a -> Boolean) -> [a] -> [[a]]
|
||
|
* @param {Function} pred The predicate that determines where the array is split.
|
||
|
* @param {Array} list The array to be split.
|
||
|
* @return {Array}
|
||
|
* @example
|
||
|
*
|
||
|
* R.splitWhenever(R.equals(2), [1, 2, 3, 2, 4, 5, 2, 6, 7]); //=> [[1], [3], [4, 5], [6, 7]]
|
||
|
*/
|
||
|
var splitWhenever = _curryN(2, [], function splitWhenever(pred, list) {
|
||
|
var acc = [];
|
||
|
var curr = [];
|
||
|
for (var i = 0;i < list.length;i = i + 1) {
|
||
|
if (!pred(list[i])) {
|
||
|
curr.push(list[i]);
|
||
|
}
|
||
|
if ((i < list.length - 1 && pred(list[i + 1]) || i === list.length - 1) && curr.length > 0) {
|
||
|
acc.push(curr);
|
||
|
curr = [];
|
||
|
}
|
||
|
}
|
||
|
return acc;
|
||
|
});
|
||
|
export default splitWhenever;
|