98275997f39313ff91771f32b9a20918398c2e26
[platform/framework/web/crosswalk-tizen.git] /
1 var baseIndexOf = require('../internal/baseIndexOf'),
2     binaryIndex = require('../internal/binaryIndex');
3
4 /* Native method references for those with the same name as other `lodash` methods. */
5 var nativeMax = Math.max;
6
7 /**
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.
12  *
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`.
16  *
17  * @static
18  * @memberOf _
19  * @category Array
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`.
25  * @example
26  *
27  * _.indexOf([1, 2, 1, 2], 2);
28  * // => 1
29  *
30  * // using `fromIndex`
31  * _.indexOf([1, 2, 1, 2], 2, 2);
32  * // => 3
33  *
34  * // performing a binary search
35  * _.indexOf([1, 1, 2, 2], 2, true);
36  * // => 2
37  */
38 function indexOf(array, value, fromIndex) {
39   var length = array ? array.length : 0;
40   if (!length) {
41     return -1;
42   }
43   if (typeof fromIndex == 'number') {
44     fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
45   } else if (fromIndex) {
46     var index = binaryIndex(array, value),
47         other = array[index];
48
49     if (value === value ? (value === other) : (other !== other)) {
50       return index;
51     }
52     return -1;
53   }
54   return baseIndexOf(array, value, fromIndex || 0);
55 }
56
57 module.exports = indexOf;