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.

42 lines
1.2 KiB

4 years ago
  1. var baseIndexOf = require('./_baseIndexOf'),
  2. toInteger = require('./toInteger');
  3. /* Built-in method references for those with the same name as other `lodash` methods. */
  4. var nativeMax = Math.max;
  5. /**
  6. * Gets the index at which the first occurrence of `value` is found in `array`
  7. * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  8. * for equality comparisons. If `fromIndex` is negative, it's used as the
  9. * offset from the end of `array`.
  10. *
  11. * @static
  12. * @memberOf _
  13. * @since 0.1.0
  14. * @category Array
  15. * @param {Array} array The array to inspect.
  16. * @param {*} value The value to search for.
  17. * @param {number} [fromIndex=0] The index to search from.
  18. * @returns {number} Returns the index of the matched value, else `-1`.
  19. * @example
  20. *
  21. * _.indexOf([1, 2, 1, 2], 2);
  22. * // => 1
  23. *
  24. * // Search from the `fromIndex`.
  25. * _.indexOf([1, 2, 1, 2], 2, 2);
  26. * // => 3
  27. */
  28. function indexOf(array, value, fromIndex) {
  29. var length = array == null ? 0 : array.length;
  30. if (!length) {
  31. return -1;
  32. }
  33. var index = fromIndex == null ? 0 : toInteger(fromIndex);
  34. if (index < 0) {
  35. index = nativeMax(length + index, 0);
  36. }
  37. return baseIndexOf(array, value, index);
  38. }
  39. module.exports = indexOf;