Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | module.exports = function (arr, predicate, ctx) { |
| 3 | if (typeof Array.prototype.findIndex === 'function') { |
| 4 | return arr.findIndex(predicate, ctx); |
| 5 | } |
| 6 | |
| 7 | if (typeof predicate !== 'function') { |
| 8 | throw new TypeError('predicate must be a function'); |
| 9 | } |
| 10 | |
| 11 | var list = Object(arr); |
| 12 | var len = list.length; |
| 13 | |
| 14 | if (len === 0) { |
| 15 | return -1; |
| 16 | } |
| 17 | |
| 18 | for (var i = 0; i < len; i++) { |
| 19 | if (predicate.call(ctx, list[i], i, list)) { |
| 20 | return i; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return -1; |
| 25 | }; |