Version 1
[yaffs-website] / node_modules / uncss / node_modules / lodash / lastIndexOf.js
1 var indexOfNaN = require('./internal/indexOfNaN'),
2     toInteger = require('./toInteger');
3
4 /* Built-in method references for those with the same name as other `lodash` methods. */
5 var nativeMax = Math.max,
6     nativeMin = Math.min;
7
8 /**
9  * This method is like `_.indexOf` except that it iterates over elements of
10  * `array` from right to left.
11  *
12  * @static
13  * @memberOf _
14  * @category Array
15  * @param {Array} array The array to search.
16  * @param {*} value The value to search for.
17  * @param {number} [fromIndex=array.length-1] The index to search from.
18  * @returns {number} Returns the index of the matched value, else `-1`.
19  * @example
20  *
21  * _.lastIndexOf([1, 2, 1, 2], 2);
22  * // => 3
23  *
24  * // using `fromIndex`
25  * _.lastIndexOf([1, 2, 1, 2], 2, 2);
26  * // => 1
27  */
28 function lastIndexOf(array, value, fromIndex) {
29   var length = array ? array.length : 0;
30   if (!length) {
31     return -1;
32   }
33   var index = length;
34   if (fromIndex !== undefined) {
35     index = toInteger(fromIndex);
36     index = (index < 0 ? nativeMax(length + index, 0) : nativeMin(index, length - 1)) + 1;
37   }
38   if (value !== value) {
39     return indexOfNaN(array, index, true);
40   }
41   while (index--) {
42     if (array[index] === value) {
43       return index;
44     }
45   }
46   return -1;
47 }
48
49 module.exports = lastIndexOf;