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` for equality comparisons. If `fromIndex` is negative,
10 * it is used as the offset from the end of `array`. If `array` is sorted
11 * providing `true` for `fromIndex` performs a faster binary search.
13 * **Note:** [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero)
14 * comparisons are like strict equality comparisons, e.g. `===`, except that
15 * `NaN` matches `NaN`.
20 * @param {Array} array The array to search.
21 * @param {*} value The value to search for.
22 * @param {boolean|number} [fromIndex=0] The index to search from or `true`
23 * to perform a binary search on a sorted array.
24 * @returns {number} Returns the index of the matched value, else `-1`.
27 * _.indexOf([1, 2, 1, 2], 2);
30 * // using `fromIndex`
31 * _.indexOf([1, 2, 1, 2], 2, 2);
34 * // performing a binary search
35 * _.indexOf([1, 1, 2, 2], 2, true);
38 function indexOf(array, value, fromIndex) {
39 var length = array ? array.length : 0;
43 if (typeof fromIndex == 'number') {
44 fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
45 } else if (fromIndex) {
46 var index = binaryIndex(array, value),
49 if (value === value ? (value === other) : (other !== other)) {
54 return baseIndexOf(array, value, fromIndex || 0);
57 module.exports = indexOf;