findLast.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _xfindLast from "./internal/_xfindLast.js";
  4. /**
  5. * Returns the last element of the list which matches the predicate, or
  6. * `undefined` if no element matches.
  7. *
  8. * Acts as a transducer if a transformer is given in list position.
  9. *
  10. * @func
  11. * @memberOf R
  12. * @since v0.1.1
  13. * @category List
  14. * @sig (a -> Boolean) -> [a] -> a | undefined
  15. * @param {Function} fn The predicate function used to determine if the element is the
  16. * desired one.
  17. * @param {Array} list The array to consider.
  18. * @return {Object} The element found, or `undefined`.
  19. * @see R.transduce
  20. * @example
  21. *
  22. * const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  23. * R.findLast(R.propEq('a', 1))(xs); //=> {a: 1, b: 1}
  24. * R.findLast(R.propEq('a', 4))(xs); //=> undefined
  25. */
  26. var findLast =
  27. /*#__PURE__*/
  28. _curry2(
  29. /*#__PURE__*/
  30. _dispatchable([], _xfindLast, function findLast(fn, list) {
  31. var idx = list.length - 1;
  32. while (idx >= 0) {
  33. if (fn(list[idx])) {
  34. return list[idx];
  35. }
  36. idx -= 1;
  37. }
  38. }));
  39. export default findLast;