findLastIndex.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import _curry2 from "./internal/_curry2.js";
  2. import _dispatchable from "./internal/_dispatchable.js";
  3. import _xfindLastIndex from "./internal/_xfindLastIndex.js";
  4. /**
  5. * Returns the index of the last element of the list which matches the
  6. * predicate, or `-1` 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] -> Number
  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 {Number} The index of the element found, or `-1`.
  19. * @see R.transduce, R.lastIndexOf
  20. * @example
  21. *
  22. * const xs = [{a: 1, b: 0}, {a:1, b: 1}];
  23. * R.findLastIndex(R.propEq('a', 1))(xs); //=> 1
  24. * R.findLastIndex(R.propEq('a', 4))(xs); //=> -1
  25. */
  26. var findLastIndex =
  27. /*#__PURE__*/
  28. _curry2(
  29. /*#__PURE__*/
  30. _dispatchable([], _xfindLastIndex, function findLastIndex(fn, list) {
  31. var idx = list.length - 1;
  32. while (idx >= 0) {
  33. if (fn(list[idx])) {
  34. return idx;
  35. }
  36. idx -= 1;
  37. }
  38. return -1;
  39. }));
  40. export default findLastIndex;