sortedLastIndexOf.js 770 B

12345678910111213141516171819202122232425262728293031
  1. var baseSortedIndex = require('./_baseSortedIndex'),
  2. eq = require('./eq');
  3. /**
  4. * This method is like `_.lastIndexOf` except that it performs a binary
  5. * search on a sorted `array`.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @since 4.0.0
  10. * @category Array
  11. * @param {Array} array The array to inspect.
  12. * @param {*} value The value to search for.
  13. * @returns {number} Returns the index of the matched value, else `-1`.
  14. * @example
  15. *
  16. * _.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
  17. * // => 3
  18. */
  19. function sortedLastIndexOf(array, value) {
  20. var length = array == null ? 0 : array.length;
  21. if (length) {
  22. var index = baseSortedIndex(array, value, true) - 1;
  23. if (eq(array[index], value)) {
  24. return index;
  25. }
  26. }
  27. return -1;
  28. }
  29. module.exports = sortedLastIndexOf;