splitWhen.js 1.0 KB

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