[6a3a178] | 1 | var baseFindIndex = require('./_baseFindIndex'),
|
---|
| 2 | baseIteratee = require('./_baseIteratee'),
|
---|
| 3 | toInteger = require('./toInteger');
|
---|
| 4 |
|
---|
| 5 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
---|
| 6 | var nativeMax = Math.max,
|
---|
| 7 | nativeMin = Math.min;
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * This method is like `_.findIndex` except that it iterates over elements
|
---|
| 11 | * of `collection` from right to left.
|
---|
| 12 | *
|
---|
| 13 | * @static
|
---|
| 14 | * @memberOf _
|
---|
| 15 | * @since 2.0.0
|
---|
| 16 | * @category Array
|
---|
| 17 | * @param {Array} array The array to inspect.
|
---|
| 18 | * @param {Function} [predicate=_.identity] The function invoked per iteration.
|
---|
| 19 | * @param {number} [fromIndex=array.length-1] The index to search from.
|
---|
| 20 | * @returns {number} Returns the index of the found element, else `-1`.
|
---|
| 21 | * @example
|
---|
| 22 | *
|
---|
| 23 | * var users = [
|
---|
| 24 | * { 'user': 'barney', 'active': true },
|
---|
| 25 | * { 'user': 'fred', 'active': false },
|
---|
| 26 | * { 'user': 'pebbles', 'active': false }
|
---|
| 27 | * ];
|
---|
| 28 | *
|
---|
| 29 | * _.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
|
---|
| 30 | * // => 2
|
---|
| 31 | *
|
---|
| 32 | * // The `_.matches` iteratee shorthand.
|
---|
| 33 | * _.findLastIndex(users, { 'user': 'barney', 'active': true });
|
---|
| 34 | * // => 0
|
---|
| 35 | *
|
---|
| 36 | * // The `_.matchesProperty` iteratee shorthand.
|
---|
| 37 | * _.findLastIndex(users, ['active', false]);
|
---|
| 38 | * // => 2
|
---|
| 39 | *
|
---|
| 40 | * // The `_.property` iteratee shorthand.
|
---|
| 41 | * _.findLastIndex(users, 'active');
|
---|
| 42 | * // => 0
|
---|
| 43 | */
|
---|
| 44 | function findLastIndex(array, predicate, fromIndex) {
|
---|
| 45 | var length = array == null ? 0 : array.length;
|
---|
| 46 | if (!length) {
|
---|
| 47 | return -1;
|
---|
| 48 | }
|
---|
| 49 | var index = length - 1;
|
---|
| 50 | if (fromIndex !== undefined) {
|
---|
| 51 | index = toInteger(fromIndex);
|
---|
| 52 | index = fromIndex < 0
|
---|
| 53 | ? nativeMax(length + index, 0)
|
---|
| 54 | : nativeMin(index, length - 1);
|
---|
| 55 | }
|
---|
| 56 | return baseFindIndex(array, baseIteratee(predicate, 3), index, true);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | module.exports = findLastIndex;
|
---|