forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindIndex.js
41 lines (38 loc) · 1.27 KB
/
findIndex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import baseFindIndex from './.internal/baseFindIndex.js';
import toInteger from './toInteger.js';
/* Built-in method references for those with the same name as other `lodash` methods. */
const nativeMax = Math.max;
/**
* This method is like `find` except that it returns the index of the first
* element `predicate` returns truthy for instead of the element itself.
*
* @since 1.1.0
* @category Array
* @param {Array} array The array to inspect.
* @param {Function} predicate The function invoked per iteration.
* @param {number} [fromIndex=0] The index to search from.
* @returns {number} Returns the index of the found element, else `-1`.
* @see find, findKey, findLast, findLastIndex, findLastKey
* @example
*
* const users = [
* { 'user': 'barney', 'active': false },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': true }
* ];
*
* findIndex(users, ({ user }) => user == 'barney');
* // => 0
*/
function findIndex(array, predicate, fromIndex) {
const length = array == null ? 0 : array.length;
if (!length) {
return -1;
}
let index = fromIndex == null ? 0 : toInteger(fromIndex);
if (index < 0) {
index = nativeMax(length + index, 0);
}
return baseFindIndex(array, predicate, index);
}
export default findIndex;