blob: 61bff61af1a604f057ae52ad3d9852f16c5d3162 (
plain)
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
|
function findIndex(array, predicate, self) {
var len = array.length;
var i;
if (len === 0) return -1;
if (typeof predicate !== 'function') {
throw new TypeError(predicate + ' must be a function');
}
if (self) {
for (i = 0; i < len; i++) {
if (predicate.call(self, array[i], i, array)) {
return i;
}
}
} else {
for (i = 0; i < len; i++) {
if (predicate(array[i], i, array)) {
return i;
}
}
}
return -1;
}
module.exports = findIndex
|