[react-packager] Add Array.prototype.es6 polyfill

This commit is contained in:
Amjad Masad 2015-04-14 15:26:59 -07:00
Родитель f7aeefa521
Коммит 758dd0d376
3 изменённых файлов: 145 добавлений и 1 удалений

Просмотреть файл

@ -93,6 +93,18 @@ describe('HasteDependencyResolver', function() {
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/Array.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js',
],
},
module
]);
});
@ -164,6 +176,18 @@ describe('HasteDependencyResolver', function() {
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/Array.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js',
dependencies: [
'polyfills/prelude_dev.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js'
],
},
module
]);
});
@ -236,6 +260,18 @@ describe('HasteDependencyResolver', function() {
'polyfills/error-guard.js'
],
},
{ id: 'polyfills/Array.prototype.es6.js',
isPolyfill: true,
path: 'polyfills/Array.prototype.es6.js',
dependencies: [
'polyfills/prelude.js',
'polyfills/require.js',
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js',
],
},
{ path: 'some module',
id: 'some module',
isPolyfill: true,
@ -245,7 +281,8 @@ describe('HasteDependencyResolver', function() {
'polyfills/polyfills.js',
'polyfills/console.js',
'polyfills/error-guard.js',
'polyfills/String.prototype.es6.js'
'polyfills/String.prototype.es6.js',
'polyfills/Array.prototype.es6.js'
]
},
module

Просмотреть файл

@ -113,6 +113,7 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
path.join(__dirname, 'polyfills/console.js'),
path.join(__dirname, 'polyfills/error-guard.js'),
path.join(__dirname, 'polyfills/String.prototype.es6.js'),
path.join(__dirname, 'polyfills/Array.prototype.es6.js'),
].concat(this._polyfillModuleNames);
var polyfillModules = polyfillModuleNames.map(

Просмотреть файл

@ -0,0 +1,106 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @provides Array.prototype.es6
* @polyfill
* @requires __DEV__
*/
/*eslint-disable */
/*jslint bitwise: true */
(function (undefined) {
if (__DEV__) {
// Define DEV-only setter that blows up when someone incorrectly
// iterates over arrays.
try {
Object.defineProperty && Object.defineProperty(
Array.prototype,
'__ARRAY_ENUMERATION_GUARD__',
{
configurable: true,
enumerable: true,
get: function() {
console.error(
'Your code is broken! Do not iterate over arrays with ' +
'for...in. See https://fburl.com/31944000 for more information.'
);
}
}
);
} catch (e) {
// Nothing
}
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
function findIndex(predicate, context) {
/**
* Why am I seeing this "findIndex" method as a value in my array!?
*
* We polyfill the "findIndex" method -- called like
* `[1, 2, 3].findIndex(1)` -- for older browsers. A side effect of the way
* we do that is that the method is enumerable. If you were incorrectly
* iterating over your array using the object property iterator syntax
* `for (key in obj)` you will see the method name "findIndex" as a key.
*
* To fix your code please do one of the following:
*
* - Use a regular for loop with index.
* - Use one of the array methods: a.forEach, a.map, etc.
* - Guard your body of your loop with a `arr.hasOwnProperty(key)` check.
*
* More info:
* https://our.intern.facebook.com/intern/dex/qa/669736606441771/
*/
if (this == null) {
throw new TypeError(
'Array.prototype.findIndex called on null or undefined'
);
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
for (var i = 0; i < length; i++) {
if (predicate.call(context, list[i], i, list)) {
return i;
}
}
return -1;
}
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = findIndex;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
if (!Array.prototype.find) {
Array.prototype.find = function(predicate, context) {
/**
* Why am I seeing this "find" method as a value in my array!?
*
* We polyfill the "find" method -- called like
* `[1, 2, 3].find(1)` -- for older browsers. A side effect of the way
* we do that is that the method is enumerable. If you were incorrectly
* iterating over your array using the object property iterator syntax
* `for (key in obj)` you will see the method name "find" as a key.
*
* To fix your code please do one of the following:
*
* - Use a regular for loop with index.
* - Use one of the array methods: a.forEach, a.map, etc.
* - Guard your body of your loop with a `arr.hasOwnProperty(key)` check.
*
* More info:
* https://our.intern.facebook.com/intern/dex/qa/669736606441771/
*/
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
var index = findIndex.call(this, predicate, context);
return index === -1 ? undefined : this[index];
};
}
})();