UNPKG

1.08 kBJavaScriptView Raw
1var _curry2 =
2/*#__PURE__*/
3require("./internal/_curry2");
4/**
5 * Takes a list and a predicate and returns a pair of lists with the following properties:
6 *
7 * - the result of concatenating the two output lists is equivalent to the input list;
8 * - none of the elements of the first output list satisfies the predicate; and
9 * - if the second output list is non-empty, its first element satisfies the predicate.
10 *
11 * @func
12 * @memberOf R
13 * @since v0.19.0
14 * @category List
15 * @sig (a -> Boolean) -> [a] -> [[a], [a]]
16 * @param {Function} pred The predicate that determines where the array is split.
17 * @param {Array} list The array to be split.
18 * @return {Array}
19 * @example
20 *
21 * R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]]
22 */
23
24
25var splitWhen =
26/*#__PURE__*/
27_curry2(function splitWhen(pred, list) {
28 var idx = 0;
29 var len = list.length;
30 var prefix = [];
31
32 while (idx < len && !pred(list[idx])) {
33 prefix.push(list[idx]);
34 idx += 1;
35 }
36
37 return [prefix, Array.prototype.slice.call(list, idx)];
38});
39
40module.exports = splitWhen;
\No newline at end of file