1 var baseIndexOf = require('../internal/baseIndexOf'),
2 binaryIndex = require('../internal/binaryIndex');
4 /* Native method references for those with the same name as other `lodash` methods. */
5 var nativeMax = Math.max;
8 * Gets the index at which the first occurrence of `value` is found in `array`
9 * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
10 * for equality comparisons. If `fromIndex` is negative, it's used as the offset
11 * from the end of `array`. If `array` is sorted providing `true` for `fromIndex`
12 * performs a faster binary search.
17 * @param {Array} array The array to search.
18 * @param {*} value The value to search for.
19 * @param {boolean|number} [fromIndex=0] The index to search from or `true`
20 * to perform a binary search on a sorted array.
21 * @returns {number} Returns the index of the matched value, else `-1`.
24 * _.indexOf([1, 2, 1, 2], 2);
27 * // using `fromIndex`
28 * _.indexOf([1, 2, 1, 2], 2, 2);
31 * // performing a binary search
32 * _.indexOf([1, 1, 2, 2], 2, true);
35 function indexOf(array, value, fromIndex) {
36 var length = array ? array.length : 0;
40 if (typeof fromIndex == 'number') {
41 fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
42 } else if (fromIndex) {
43 var index = binaryIndex(array, value);
45 (value === value ? (value === array[index]) : (array[index] !== array[index]))) {
50 return baseIndexOf(array, value, fromIndex || 0);
53 module.exports = indexOf;