Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit c8f5a03

Browse files
committed
add findexIndex()
1 parent 49fdcc7 commit c8f5a03

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/util.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,41 @@ export function forEach(collection, callback) {
1313
callback.call(null, collection[key], key);
1414
});
1515
}
16+
17+
/**
18+
* Array.findIndex() polyfill.
19+
*/
20+
if (!Array.prototype.findIndex) {
21+
Object.defineProperty(Array.prototype, 'findIndex', {
22+
23+
value(predicate) {
24+
25+
if (this == null) {
26+
throw new TypeError('"this" is null or not defined');
27+
}
28+
29+
if (typeof predicate !== 'function') {
30+
throw new TypeError('predicate must be a function');
31+
}
32+
33+
var o = Object(this);
34+
var len = o.length >>> 0;
35+
var thisArg = arguments[1];
36+
var k = 0;
37+
38+
while (k < len) {
39+
40+
var kValue = o[k];
41+
42+
if (predicate.call(thisArg, kValue, k, o)) {
43+
return k;
44+
}
45+
46+
k++;
47+
}
48+
49+
return -1;
50+
}
51+
52+
});
53+
}

0 commit comments

Comments
 (0)