mirror of
https://github.com/status-im/react-native.git
synced 2025-01-27 01:40:08 +00:00
make Object.assign polyfill conditional and spec compliant
Summary: Replace the non-compliant `Object.assign` with a spec compliant version from Mozilla. Since its spec compliant we can apply conditionally only when a native version is not available. Reviewed By: yungsters Differential Revision: D8896359 fbshipit-source-id: 4999fe3094eba2206293bbe28760a4e46cbee6aa
This commit is contained in:
parent
1329d18c6a
commit
966bb63a80
@ -6,60 +6,34 @@
|
||||
*
|
||||
* @format
|
||||
* @polyfill
|
||||
* @nolint
|
||||
*/
|
||||
|
||||
// WARNING: This is an optimized version that fails on hasOwnProperty checks
|
||||
// and non objects. It's not spec-compliant. It's a perf optimization.
|
||||
// This is only needed for iOS 8 and current Android JSC.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
||||
|
||||
Object.assign = function(target, sources) {
|
||||
if (__DEV__) {
|
||||
if (target == null) {
|
||||
throw new TypeError('Object.assign target cannot be null or undefined');
|
||||
}
|
||||
if (typeof target !== 'object' && typeof target !== 'function') {
|
||||
throw new TypeError(
|
||||
'In this environment the target of assign MUST be an object. ' +
|
||||
'This error is a performance optimization and not spec compliant.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (var nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
|
||||
var nextSource = arguments[nextIndex];
|
||||
if (nextSource == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (__DEV__) {
|
||||
if (typeof nextSource !== 'object' && typeof nextSource !== 'function') {
|
||||
throw new TypeError(
|
||||
'In this environment the sources for assign MUST be an object. ' +
|
||||
'This error is a performance optimization and not spec compliant.',
|
||||
);
|
||||
if (typeof Object.assign !== 'function') {
|
||||
Object.defineProperty(Object, 'assign', {
|
||||
value: function assign(target, varArgs) {
|
||||
'use strict';
|
||||
if (target == null) {
|
||||
throw new TypeError('Cannot convert undefined or null to object');
|
||||
}
|
||||
}
|
||||
|
||||
// We don't currently support accessors nor proxies. Therefore this
|
||||
// copy cannot throw. If we ever supported this then we must handle
|
||||
// exceptions and side-effects.
|
||||
let to = Object(target);
|
||||
|
||||
for (var key in nextSource) {
|
||||
if (__DEV__) {
|
||||
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
if (!hasOwnProperty.call(nextSource, key)) {
|
||||
throw new TypeError(
|
||||
'One of the sources for assign has an enumerable key on the ' +
|
||||
'prototype chain. Are you trying to assign a prototype property? ' +
|
||||
"We don't allow it, as this is an edge case that we do not support. " +
|
||||
'This error is a performance optimization and not spec compliant.',
|
||||
);
|
||||
for (let index = 1; index < arguments.length; index++) {
|
||||
let nextSource = arguments[index];
|
||||
|
||||
if (nextSource != null) {
|
||||
for (let nextKey in nextSource) {
|
||||
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
||||
to[nextKey] = nextSource[nextKey];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
target[key] = nextSource[key];
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
return to;
|
||||
},
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user