|
| 1 | +/** |
| 2 | + * Copyright 2004-present Facebook. All Rights Reserved. |
| 3 | + * |
| 4 | + * @provides Array.prototype.es6 |
| 5 | + * @polyfill |
| 6 | + * @requires __DEV__ |
| 7 | + */ |
| 8 | + |
| 9 | +/*eslint-disable */ |
| 10 | +/*jslint bitwise: true */ |
| 11 | + |
| 12 | +(function (undefined) { |
| 13 | + if (__DEV__) { |
| 14 | + // Define DEV-only setter that blows up when someone incorrectly |
| 15 | + // iterates over arrays. |
| 16 | + try { |
| 17 | + Object.defineProperty && Object.defineProperty( |
| 18 | + Array.prototype, |
| 19 | + '__ARRAY_ENUMERATION_GUARD__', |
| 20 | + { |
| 21 | + configurable: true, |
| 22 | + enumerable: true, |
| 23 | + get: function() { |
| 24 | + console.error( |
| 25 | + 'Your code is broken! Do not iterate over arrays with ' + |
| 26 | + 'for...in. See https://fburl.com/31944000 for more information.' |
| 27 | + ); |
| 28 | + } |
| 29 | + } |
| 30 | + ); |
| 31 | + } catch (e) { |
| 32 | + // Nothing |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex |
| 37 | + function findIndex(predicate, context) { |
| 38 | + /** |
| 39 | + * Why am I seeing this "findIndex" method as a value in my array!? |
| 40 | + * |
| 41 | + * We polyfill the "findIndex" method -- called like |
| 42 | + * `[1, 2, 3].findIndex(1)` -- for older browsers. A side effect of the way |
| 43 | + * we do that is that the method is enumerable. If you were incorrectly |
| 44 | + * iterating over your array using the object property iterator syntax |
| 45 | + * `for (key in obj)` you will see the method name "findIndex" as a key. |
| 46 | + * |
| 47 | + * To fix your code please do one of the following: |
| 48 | + * |
| 49 | + * - Use a regular for loop with index. |
| 50 | + * - Use one of the array methods: a.forEach, a.map, etc. |
| 51 | + * - Guard your body of your loop with a `arr.hasOwnProperty(key)` check. |
| 52 | + * |
| 53 | + * More info: |
| 54 | + * https://our.intern.facebook.com/intern/dex/qa/669736606441771/ |
| 55 | + */ |
| 56 | + if (this == null) { |
| 57 | + throw new TypeError( |
| 58 | + 'Array.prototype.findIndex called on null or undefined' |
| 59 | + ); |
| 60 | + } |
| 61 | + if (typeof predicate !== 'function') { |
| 62 | + throw new TypeError('predicate must be a function'); |
| 63 | + } |
| 64 | + var list = Object(this); |
| 65 | + var length = list.length >>> 0; |
| 66 | + for (var i = 0; i < length; i++) { |
| 67 | + if (predicate.call(context, list[i], i, list)) { |
| 68 | + return i; |
| 69 | + } |
| 70 | + } |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + if (!Array.prototype.findIndex) { |
| 75 | + Array.prototype.findIndex = findIndex; |
| 76 | + } |
| 77 | + |
| 78 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find |
| 79 | + if (!Array.prototype.find) { |
| 80 | + Array.prototype.find = function(predicate, context) { |
| 81 | + /** |
| 82 | + * Why am I seeing this "find" method as a value in my array!? |
| 83 | + * |
| 84 | + * We polyfill the "find" method -- called like |
| 85 | + * `[1, 2, 3].find(1)` -- for older browsers. A side effect of the way |
| 86 | + * we do that is that the method is enumerable. If you were incorrectly |
| 87 | + * iterating over your array using the object property iterator syntax |
| 88 | + * `for (key in obj)` you will see the method name "find" as a key. |
| 89 | + * |
| 90 | + * To fix your code please do one of the following: |
| 91 | + * |
| 92 | + * - Use a regular for loop with index. |
| 93 | + * - Use one of the array methods: a.forEach, a.map, etc. |
| 94 | + * - Guard your body of your loop with a `arr.hasOwnProperty(key)` check. |
| 95 | + * |
| 96 | + * More info: |
| 97 | + * https://our.intern.facebook.com/intern/dex/qa/669736606441771/ |
| 98 | + */ |
| 99 | + if (this == null) { |
| 100 | + throw new TypeError('Array.prototype.find called on null or undefined'); |
| 101 | + } |
| 102 | + var index = findIndex.call(this, predicate, context); |
| 103 | + return index === -1 ? undefined : this[index]; |
| 104 | + }; |
| 105 | + } |
| 106 | +})(); |
0 commit comments