mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Sync internal modules
We've actually diverged more with some modules, but we don't want a cascade of dependencies out here. Mostly, the changes internally that we don't want are tied to FB infrastructure but otherwise are functionally equivalent (usually around error reporting, code monitoring).
This commit is contained in:
+5
-5
@@ -6,7 +6,7 @@
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule createArrayFrom
|
||||
* @providesModule createArrayFromMixed
|
||||
* @typechecks
|
||||
*/
|
||||
|
||||
@@ -57,10 +57,10 @@ function hasArrayNature(obj) {
|
||||
*
|
||||
* This is mostly useful idiomatically:
|
||||
*
|
||||
* var createArrayFrom = require('createArrayFrom');
|
||||
* var createArrayFromMixed = require('createArrayFromMixed');
|
||||
*
|
||||
* function takesOneOrMoreThings(things) {
|
||||
* things = createArrayFrom(things);
|
||||
* things = createArrayFromMixed(things);
|
||||
* ...
|
||||
* }
|
||||
*
|
||||
@@ -72,7 +72,7 @@ function hasArrayNature(obj) {
|
||||
* @param {*} obj
|
||||
* @return {array}
|
||||
*/
|
||||
function createArrayFrom(obj) {
|
||||
function createArrayFromMixed(obj) {
|
||||
if (!hasArrayNature(obj)) {
|
||||
return [obj];
|
||||
} else if (Array.isArray(obj)) {
|
||||
@@ -82,4 +82,4 @@ function createArrayFrom(obj) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = createArrayFrom;
|
||||
module.exports = createArrayFromMixed;
|
||||
+3
-3
@@ -14,7 +14,7 @@
|
||||
|
||||
var ExecutionEnvironment = require('ExecutionEnvironment');
|
||||
|
||||
var createArrayFrom = require('createArrayFrom');
|
||||
var createArrayFromMixed = require('createArrayFromMixed');
|
||||
var getMarkupWrap = require('getMarkupWrap');
|
||||
var invariant = require('invariant');
|
||||
|
||||
@@ -73,10 +73,10 @@ function createNodesFromMarkup(markup, handleScript) {
|
||||
handleScript,
|
||||
'createNodesFromMarkup(...): Unexpected <script> element rendered.'
|
||||
);
|
||||
createArrayFrom(scripts).forEach(handleScript);
|
||||
createArrayFromMixed(scripts).forEach(handleScript);
|
||||
}
|
||||
|
||||
var nodes = createArrayFrom(node.childNodes);
|
||||
var nodes = createArrayFromMixed(node.childNodes);
|
||||
while (node.lastChild) {
|
||||
node.removeChild(node.lastChild);
|
||||
}
|
||||
|
||||
Vendored
-46
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013-2015, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule mergeDeep
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var mergeHelpers = require('mergeHelpers');
|
||||
var mergeDeepInto = require('mergeDeepInto');
|
||||
|
||||
var checkArrayStrategy = mergeHelpers.checkArrayStrategy;
|
||||
var checkMergeObjectArgs = mergeHelpers.checkMergeObjectArgs;
|
||||
var normalizeMergeArg = mergeHelpers.normalizeMergeArg;
|
||||
|
||||
/**
|
||||
* @see mergeDeepImpl comments.
|
||||
*
|
||||
* @param {!Object} one returned will be an structural extension of this.
|
||||
* @param {!Object} two values from two take precedence over values in one.
|
||||
* @param {?Enum=} arrayStrategy One of `arrayStrategies`.
|
||||
* @return {!Object} the extension of one by two.
|
||||
*/
|
||||
var mergeDeep = function(oneParam, twoParam, arrayStrategy) {
|
||||
var one = normalizeMergeArg(oneParam);
|
||||
var two = normalizeMergeArg(twoParam);
|
||||
checkMergeObjectArgs(one, two);
|
||||
checkArrayStrategy(arrayStrategy);
|
||||
var newObj = {};
|
||||
// TODO: This is horribly inefficient. Even when some deep object in `one`
|
||||
// will be clobbered by another entity in `two`, we perform a clone of that
|
||||
// entire structure. To make this code more efficient, we'll need to either
|
||||
// enhance `mergeDeepInto` to accept multiple arguments or mirror that code
|
||||
// here.
|
||||
mergeDeepInto(newObj, one, arrayStrategy);
|
||||
mergeDeepInto(newObj, two, arrayStrategy);
|
||||
return newObj;
|
||||
};
|
||||
|
||||
|
||||
module.exports = mergeDeep;
|
||||
Vendored
-220
@@ -1,220 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013-2015, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule mergeDeepInto
|
||||
*/
|
||||
|
||||
// Empty blocks improve readability so disable that warning
|
||||
// jshint -W035
|
||||
|
||||
"use strict";
|
||||
|
||||
var invariant = require('invariant');
|
||||
var mergeHelpers = require('mergeHelpers');
|
||||
|
||||
var ArrayStrategies = mergeHelpers.ArrayStrategies;
|
||||
var checkArrayStrategy = mergeHelpers.checkArrayStrategy;
|
||||
var checkMergeArrayArgs = mergeHelpers.checkMergeArrayArgs;
|
||||
var checkMergeLevel = mergeHelpers.checkMergeLevel;
|
||||
var checkMergeObjectArgs = mergeHelpers.checkMergeObjectArgs;
|
||||
var isTerminal = mergeHelpers.isTerminal;
|
||||
var normalizeMergeArg = mergeHelpers.normalizeMergeArg;
|
||||
|
||||
/**
|
||||
* Every deep merge function must handle merging in each of the following cases
|
||||
* at every level. We may refer to letters below in implementations. For each
|
||||
* case listed, the "Result" listed describes the *value* of the result, but
|
||||
* does not specify anything about the memory graph of the result. In other
|
||||
* words the Results listed below will be the same for `merge` and `mergeInto`
|
||||
* but `merge` may have different guarantees about mutation/cloning. In the
|
||||
* table, "Object" refers to a non- Array, non-terminal object. One result is
|
||||
* undefined and requires that the caller specify the resolution policy (only
|
||||
* when trying to merge two arrays).
|
||||
*
|
||||
* Scenario Result
|
||||
* --------------------------------------------------------------
|
||||
* [A]: (terminal, terminal) right terminal
|
||||
* [B]: (terminal, Array) right Array
|
||||
* [C]: (terminal, Object) right Object
|
||||
* [D]: (terminal, not present) left terminal
|
||||
* [E]: (Array, terminal) right terminal
|
||||
* [F]: (Array, Array) UNDEFINED - MUST SPECIFY POLICY
|
||||
* [G]: (Array, Object) right Object
|
||||
* [H]: (Array, not present) left Array
|
||||
* [I]: (Object, terminal) right terminal
|
||||
* [J]: (Object, Array) right Array
|
||||
* [K]: (Object, Object) merge of left and right Objects
|
||||
* [L]: (Object, not present) left Object
|
||||
* [M]: (not present, terminal) right terminal
|
||||
* [N]: (not present, Array) right Array
|
||||
* [O]: (not present, Object) right Object
|
||||
*
|
||||
* All merge functions are only expected to reason about "own" properties and,
|
||||
* any prototypical properties should have no effect on the result. At the first
|
||||
* level of recursion in deep merges, we may choose to normalize arguments
|
||||
* (convert undefined to empty objects etc.) The above chart does not describe
|
||||
* the result of those top level operations which behave specially due to the
|
||||
* normalization.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Deep merge implementation for non-Array, non-terminal Objects.
|
||||
*
|
||||
* @param {!Object} one non-Array, non-terminal Object to be mutated deeply.
|
||||
* @param {!Object} two non-Array, non-terminal taking precedence over `one`.
|
||||
* @param {?Enum=} arrayStrategy one of `arrayStrategies`.
|
||||
* @param {!number} level The level of recursion.
|
||||
*/
|
||||
var mergeDeepIntoObjects = function(one, two, arrayStrategy, level) {
|
||||
checkMergeObjectArgs(one, two);
|
||||
checkMergeLevel(level);
|
||||
var twoKeys = two ? Object.keys(two) : [];
|
||||
for (var i = 0; i < twoKeys.length; i++) {
|
||||
var twoKey = twoKeys[i];
|
||||
mergeSingleFieldDeep(one, two, twoKey, arrayStrategy, level);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deep merge implementation for Arrays.
|
||||
*
|
||||
* @param {!Array} one Array to deep merge.
|
||||
* @param {!Array} two Array to deep merge "into" `one`.
|
||||
* @param {?Enum=} arrayStrategy one of `arrayStrategies`.
|
||||
* @param {!number} level Level of recursion.
|
||||
*/
|
||||
var mergeDeepIntoArrays = function(one, two, arrayStrategy, level) {
|
||||
checkMergeArrayArgs(one, two);
|
||||
checkMergeLevel(level);
|
||||
|
||||
var maxLen = Math.max(one.length, two.length);
|
||||
for (var i = 0; i < maxLen; i++) {
|
||||
mergeSingleFieldDeep(one, two, i, arrayStrategy, level);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Given two truthy containers `one` and `two`, and a `key`, performs a deep
|
||||
* merge on a logical field.
|
||||
*
|
||||
* @param {!Array|Object} one Container to merge into.
|
||||
* @param {!Array|Object} two Container to merge from.
|
||||
* @param {!string} key Key of field that should be merged.
|
||||
* @param {lEnum=} arrayStrategy One of `arrayStrategies`.
|
||||
* @param {!number} level Current level of recursion.
|
||||
*/
|
||||
var mergeSingleFieldDeep = function(one, two, key, arrayStrategy, level) {
|
||||
var twoVal = two[key];
|
||||
var twoValIsPresent = two.hasOwnProperty(key);
|
||||
var twoValIsTerminal = twoValIsPresent && isTerminal(twoVal);
|
||||
var twoValIsArray = twoValIsPresent && Array.isArray(twoVal);
|
||||
var twoValIsProperObject =
|
||||
twoValIsPresent && !twoValIsArray && !twoValIsArray;
|
||||
|
||||
var oneVal = one[key];
|
||||
var oneValIsPresent = one.hasOwnProperty(key);
|
||||
var oneValIsTerminal = oneValIsPresent && isTerminal(oneVal);
|
||||
var oneValIsArray = oneValIsPresent && Array.isArray(oneVal);
|
||||
var oneValIsProperObject =
|
||||
oneValIsPresent && !oneValIsArray && !oneValIsArray;
|
||||
|
||||
if (oneValIsTerminal) {
|
||||
if (twoValIsTerminal) { // [A]
|
||||
one[key] = twoVal;
|
||||
} else if (twoValIsArray) { // [B]
|
||||
one[key] = [];
|
||||
mergeDeepIntoArrays(one[key], twoVal, arrayStrategy, level + 1);
|
||||
} else if (twoValIsProperObject) { // [C]
|
||||
one[key] = {};
|
||||
mergeDeepIntoObjects(one[key], twoVal, arrayStrategy, level + 1);
|
||||
} else if (!twoValIsPresent) { // [D]
|
||||
one[key] = oneVal;
|
||||
}
|
||||
} else if (oneValIsArray) {
|
||||
if (twoValIsTerminal) { // [E]
|
||||
one[key] = twoVal;
|
||||
} else if (twoValIsArray) { // [F]
|
||||
invariant(
|
||||
ArrayStrategies[arrayStrategy],
|
||||
'mergeDeepInto(...): Attempted to merge two arrays, but a valid ' +
|
||||
'ArrayStrategy was not specified.'
|
||||
);
|
||||
// Else: At this point, the only other valid option is `IndexByIndex`
|
||||
if (arrayStrategy === ArrayStrategies.Clobber) {
|
||||
oneVal.length = 0;
|
||||
}
|
||||
mergeDeepIntoArrays(oneVal, twoVal, arrayStrategy, level + 1);
|
||||
} else if (twoValIsProperObject) { // [G]
|
||||
one[key] = {};
|
||||
mergeDeepIntoObjects(one[key], twoVal, arrayStrategy, level + 1);
|
||||
} else if (!twoValIsPresent) { // [H]
|
||||
// Leave the left Array alone
|
||||
}
|
||||
} else if (oneValIsProperObject) {
|
||||
if (twoValIsTerminal) { // [I]
|
||||
one[key] = twoVal;
|
||||
} else if (twoValIsArray) { // [J]
|
||||
one[key] = [];
|
||||
mergeDeepIntoArrays(one[key], twoVal, arrayStrategy, level + 1);
|
||||
} else if (twoValIsProperObject) { // [K]
|
||||
mergeDeepIntoObjects(oneVal, twoVal, arrayStrategy, level + 1);
|
||||
} else if (!twoValIsPresent) { // [L]
|
||||
// Leave the left Object alone
|
||||
}
|
||||
} else if (!oneValIsPresent) {
|
||||
if (twoValIsTerminal) { // [M]
|
||||
one[key] = twoVal;
|
||||
} else if (twoValIsArray) { // [N]
|
||||
one[key] = [];
|
||||
mergeDeepIntoArrays(one[key], twoVal, arrayStrategy, level + 1);
|
||||
} else if (twoValIsProperObject) { // [O]
|
||||
one[key] = {};
|
||||
mergeDeepIntoObjects(one[key], twoVal, arrayStrategy, level + 1);
|
||||
} else if (!twoValIsPresent) {
|
||||
// Could/should never happen
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Provides same functionality as mergeInto, but merges by mutating the first
|
||||
* argument. Will never mutate the second argument.
|
||||
*
|
||||
* mergeDeepInto provides two guarantees:
|
||||
* 1. In the process of mutating one, will not mutate two.
|
||||
* 2. Will not cause any nonTerminal memory to be shared between one and two.
|
||||
* This means that no further mutations to one will effect two (unless some
|
||||
* other part of your application forms shared memory between one and two).
|
||||
*
|
||||
* mergeDeepInto does not guarantee that it will *preserve* any shared memory
|
||||
* between two and one that existed before invocation. After calling
|
||||
* mergeDeepInto, there will be less (or equal) amount of shared memory between
|
||||
* one and two.
|
||||
*
|
||||
* Will tolerate a circular structure as the first parameter, but not the
|
||||
* second.
|
||||
*
|
||||
* Requires that first parameter be a non-Array, non-terminal `Object`, and the
|
||||
* second argument either be an `Object` or `null/undefined`. Arrays may exist
|
||||
* in the depths of either `Object` as long as an `arrayStrategy` is supplied.
|
||||
*
|
||||
* The third parameter indicates how merging two `Array`s should be performed.
|
||||
*
|
||||
* @param {!Object} one Object to be mutated deeply.
|
||||
* @param {?Object} two Values from two take precedence over values in one.
|
||||
* @param {?Enum=} arrayStrategy One of arrayStrategy
|
||||
*/
|
||||
var mergeDeepInto = function(one, twoParam, arrayStrategy) {
|
||||
var two = normalizeMergeArg(twoParam);
|
||||
checkArrayStrategy(arrayStrategy); // Will be checked twice, for now.
|
||||
mergeDeepIntoObjects(one, two, arrayStrategy, 0);
|
||||
};
|
||||
|
||||
module.exports = mergeDeepInto;
|
||||
Vendored
-140
@@ -1,140 +0,0 @@
|
||||
/**
|
||||
* Copyright 2013-2015, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule mergeHelpers
|
||||
*
|
||||
* requiresPolyfills: Array.isArray
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var invariant = require('invariant');
|
||||
var keyMirror = require('keyMirror');
|
||||
|
||||
/**
|
||||
* Maximum number of levels to traverse. Will catch circular structures.
|
||||
* @const
|
||||
*/
|
||||
var MAX_MERGE_DEPTH = 36;
|
||||
|
||||
/**
|
||||
* We won't worry about edge cases like new String('x') or new Boolean(true).
|
||||
* Functions are considered terminals, and arrays are not.
|
||||
* @param {*} o The item/object/value to test.
|
||||
* @return {boolean} true iff the argument is a terminal.
|
||||
*/
|
||||
var isTerminal = function(o) {
|
||||
return typeof o !== 'object' || o === null;
|
||||
};
|
||||
|
||||
var mergeHelpers = {
|
||||
|
||||
MAX_MERGE_DEPTH: MAX_MERGE_DEPTH,
|
||||
|
||||
isTerminal: isTerminal,
|
||||
|
||||
/**
|
||||
* Converts null/undefined values into empty object.
|
||||
*
|
||||
* @param {?Object=} arg Argument to be normalized (nullable optional)
|
||||
* @return {!Object}
|
||||
*/
|
||||
normalizeMergeArg: function(arg) {
|
||||
return arg === undefined || arg === null ? {} : arg;
|
||||
},
|
||||
|
||||
/**
|
||||
* If merging Arrays, a merge strategy *must* be supplied. If not, it is
|
||||
* likely the caller's fault. If this function is ever called with anything
|
||||
* but `one` and `two` being `Array`s, it is the fault of the merge utilities.
|
||||
*
|
||||
* @param {*} one Array to merge into.
|
||||
* @param {*} two Array to merge from.
|
||||
*/
|
||||
checkMergeArrayArgs: function(one, two) {
|
||||
invariant(
|
||||
Array.isArray(one) && Array.isArray(two),
|
||||
'Tried to merge arrays, instead got %s and %s.',
|
||||
one,
|
||||
two
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {*} one Object to merge into.
|
||||
* @param {*} two Object to merge from.
|
||||
*/
|
||||
checkMergeObjectArgs: function(one, two) {
|
||||
mergeHelpers.checkMergeObjectArg(one);
|
||||
mergeHelpers.checkMergeObjectArg(two);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {*} arg
|
||||
*/
|
||||
checkMergeObjectArg: function(arg) {
|
||||
invariant(
|
||||
!isTerminal(arg) && !Array.isArray(arg),
|
||||
'Tried to merge an object, instead got %s.',
|
||||
arg
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {*} arg
|
||||
*/
|
||||
checkMergeIntoObjectArg: function(arg) {
|
||||
invariant(
|
||||
(!isTerminal(arg) || typeof arg === 'function') && !Array.isArray(arg),
|
||||
'Tried to merge into an object, instead got %s.',
|
||||
arg
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks that a merge was not given a circular object or an object that had
|
||||
* too great of depth.
|
||||
*
|
||||
* @param {number} Level of recursion to validate against maximum.
|
||||
*/
|
||||
checkMergeLevel: function(level) {
|
||||
invariant(
|
||||
level < MAX_MERGE_DEPTH,
|
||||
'Maximum deep merge depth exceeded. You may be attempting to merge ' +
|
||||
'circular structures in an unsupported way.'
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks that the supplied merge strategy is valid.
|
||||
*
|
||||
* @param {string} Array merge strategy.
|
||||
*/
|
||||
checkArrayStrategy: function(strategy) {
|
||||
invariant(
|
||||
strategy === undefined || strategy in mergeHelpers.ArrayStrategies,
|
||||
'You must provide an array strategy to deep merge functions to ' +
|
||||
'instruct the deep merge how to resolve merging two arrays.'
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Set of possible behaviors of merge algorithms when encountering two Arrays
|
||||
* that must be merged together.
|
||||
* - `clobber`: The left `Array` is ignored.
|
||||
* - `indexByIndex`: The result is achieved by recursively deep merging at
|
||||
* each index. (not yet supported.)
|
||||
*/
|
||||
ArrayStrategies: keyMirror({
|
||||
Clobber: true,
|
||||
IndexByIndex: true
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
module.exports = mergeHelpers;
|
||||
Vendored
+1
-1
@@ -16,7 +16,7 @@ var invariant = require('invariant');
|
||||
* Convert array-like objects to arrays.
|
||||
*
|
||||
* This API assumes the caller knows the contents of the data type. For less
|
||||
* well defined inputs use createArrayFrom.
|
||||
* well defined inputs use createArrayFromMixed.
|
||||
*
|
||||
* @param {object|function|filelist} obj
|
||||
* @return {array}
|
||||
|
||||
Reference in New Issue
Block a user