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.

40 lines
1.1 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import slice from "./slice.js";
  3. /**
  4. * Returns a new list containing the last `n` elements of a given list, passing
  5. * each value to the supplied predicate function, and terminating when the
  6. * predicate function returns `false`. Excludes the element that caused the
  7. * predicate function to fail. The predicate function is passed one argument:
  8. * *(value)*.
  9. *
  10. * @func
  11. * @memberOf R
  12. * @since v0.16.0
  13. * @category List
  14. * @sig (a -> Boolean) -> [a] -> [a]
  15. * @sig (a -> Boolean) -> String -> String
  16. * @param {Function} fn The function called per iteration.
  17. * @param {Array} xs The collection to iterate over.
  18. * @return {Array} A new array.
  19. * @see R.dropLastWhile, R.addIndex
  20. * @example
  21. *
  22. * const isNotOne = x => x !== 1;
  23. *
  24. * R.takeLastWhile(isNotOne, [1, 2, 3, 4]); //=> [2, 3, 4]
  25. *
  26. * R.takeLastWhile(x => x !== 'R' , 'Ramda'); //=> 'amda'
  27. */
  28. var takeLastWhile =
  29. /*#__PURE__*/
  30. _curry2(function takeLastWhile(fn, xs) {
  31. var idx = xs.length - 1;
  32. while (idx >= 0 && fn(xs[idx])) {
  33. idx -= 1;
  34. }
  35. return slice(idx + 1, Infinity, xs);
  36. });
  37. export default takeLastWhile;