You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 regels
1.4 KiB

4 jaren geleden
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _xtakeWhile from "./internal/_xtakeWhile.js";
  4. import slice from "./slice.js";
  5. /**
  6. * Returns a new list containing the first `n` elements of a given list,
  7. * passing each value to the supplied predicate function, and terminating when
  8. * the predicate function returns `false`. Excludes the element that caused the
  9. * predicate function to fail. The predicate function is passed one argument:
  10. * *(value)*.
  11. *
  12. * Dispatches to the `takeWhile` method of the second argument, if present.
  13. *
  14. * Acts as a transducer if a transformer is given in list position.
  15. *
  16. * @func
  17. * @memberOf R
  18. * @since v0.1.0
  19. * @category List
  20. * @sig (a -> Boolean) -> [a] -> [a]
  21. * @sig (a -> Boolean) -> String -> String
  22. * @param {Function} fn The function called per iteration.
  23. * @param {Array} xs The collection to iterate over.
  24. * @return {Array} A new array.
  25. * @see R.dropWhile, R.transduce, R.addIndex
  26. * @example
  27. *
  28. * const isNotFour = x => x !== 4;
  29. *
  30. * R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3]
  31. *
  32. * R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram'
  33. */
  34. var takeWhile =
  35. /*#__PURE__*/
  36. _curry2(
  37. /*#__PURE__*/
  38. _dispatchable(['takeWhile'], _xtakeWhile, function takeWhile(fn, xs) {
  39. var idx = 0;
  40. var len = xs.length;
  41. while (idx < len && fn(xs[idx])) {
  42. idx += 1;
  43. }
  44. return slice(0, idx, xs);
  45. }));
  46. export default takeWhile;