lastIndexOf.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import _curry2 from "./internal/_curry2.js";
  2. import _isArray from "./internal/_isArray.js";
  3. import equals from "./equals.js";
  4. /**
  5. * Returns the position of the last occurrence of an item in an array, or -1 if
  6. * the item is not included in the array. [`R.equals`](#equals) is used to
  7. * determine equality.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.1.0
  12. * @category List
  13. * @sig a -> [a] -> Number
  14. * @param {*} target The item to find.
  15. * @param {Array} xs The array to search in.
  16. * @return {Number} the index of the target, or -1 if the target is not found.
  17. * @see R.indexOf, R.findLastIndex
  18. * @example
  19. *
  20. * R.lastIndexOf(3, [-1,3,3,0,1,2,3,4]); //=> 6
  21. * R.lastIndexOf(10, [1,2,3,4]); //=> -1
  22. */
  23. var lastIndexOf =
  24. /*#__PURE__*/
  25. _curry2(function lastIndexOf(target, xs) {
  26. if (typeof xs.lastIndexOf === 'function' && !_isArray(xs)) {
  27. return xs.lastIndexOf(target);
  28. } else {
  29. var idx = xs.length - 1;
  30. while (idx >= 0) {
  31. if (equals(xs[idx], target)) {
  32. return idx;
  33. }
  34. idx -= 1;
  35. }
  36. return -1;
  37. }
  38. });
  39. export default lastIndexOf;