mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 09:27:49 +00:00
This commit is contained in:
52
lodash.js
52
lodash.js
@@ -2954,10 +2954,11 @@
|
||||
* @private
|
||||
* @param {Object} object The destination object.
|
||||
* @param {Object} source The source object.
|
||||
* @param {number} srcIndex The index of `source`.
|
||||
* @param {Function} [customizer] The function to customize merged values.
|
||||
* @param {Object} [stack] Tracks traversed source values and their merged counterparts.
|
||||
*/
|
||||
function baseMerge(object, source, customizer, stack) {
|
||||
function baseMerge(object, source, srcIndex, customizer, stack) {
|
||||
if (object === source) {
|
||||
return;
|
||||
}
|
||||
@@ -2969,7 +2970,7 @@
|
||||
}
|
||||
if (isObject(srcValue)) {
|
||||
stack || (stack = new Stack);
|
||||
baseMergeDeep(object, source, key, baseMerge, customizer, stack);
|
||||
baseMergeDeep(object, source, key, srcIndex, baseMerge, customizer, stack);
|
||||
}
|
||||
else {
|
||||
var newValue = customizer ? customizer(object[key], srcValue, (key + ''), object, source, stack) : undefined;
|
||||
@@ -2990,11 +2991,12 @@
|
||||
* @param {Object} object The destination object.
|
||||
* @param {Object} source The source object.
|
||||
* @param {string} key The key of the value to merge.
|
||||
* @param {number} srcIndex The index of `source`.
|
||||
* @param {Function} mergeFunc The function to merge values.
|
||||
* @param {Function} [customizer] The function to customize assigned values.
|
||||
* @param {Object} [stack] Tracks traversed source values and their merged counterparts.
|
||||
*/
|
||||
function baseMergeDeep(object, source, key, mergeFunc, customizer, stack) {
|
||||
function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) {
|
||||
var objValue = object[key],
|
||||
srcValue = source[key],
|
||||
stacked = stack.get(srcValue) || stack.get(objValue);
|
||||
@@ -3009,24 +3011,36 @@
|
||||
if (isCommon) {
|
||||
newValue = srcValue;
|
||||
if (isArray(srcValue) || isTypedArray(srcValue)) {
|
||||
newValue = isArray(objValue)
|
||||
? objValue
|
||||
: ((isArrayLikeObject(objValue)) ? copyArray(objValue) : baseClone(srcValue));
|
||||
if (isArray(objValue)) {
|
||||
newValue = srcIndex ? copyArray(objValue) : objValue;
|
||||
}
|
||||
else if (isArrayLikeObject(objValue)) {
|
||||
newValue = copyArray(objValue);
|
||||
}
|
||||
else {
|
||||
newValue = baseClone(srcValue);
|
||||
}
|
||||
}
|
||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||
newValue = isArguments(objValue)
|
||||
? toPlainObject(objValue)
|
||||
: (isObject(objValue) ? objValue : baseClone(srcValue));
|
||||
if (isArguments(objValue)) {
|
||||
newValue = toPlainObject(objValue);
|
||||
}
|
||||
else if (!isObject(objValue) || (srcIndex && isFunction(objValue))) {
|
||||
newValue = baseClone(srcValue);
|
||||
}
|
||||
else {
|
||||
newValue = srcIndex ? baseClone(objValue) : objValue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
isCommon = isFunction(srcValue);
|
||||
isCommon = false;
|
||||
}
|
||||
}
|
||||
stack.set(srcValue, newValue);
|
||||
|
||||
if (isCommon) {
|
||||
// Recursively merge objects and arrays (susceptible to call stack limits).
|
||||
mergeFunc(newValue, srcValue, customizer, stack);
|
||||
mergeFunc(newValue, srcValue, srcIndex, customizer, stack);
|
||||
}
|
||||
assignMergeValue(object, key, newValue);
|
||||
}
|
||||
@@ -3878,7 +3892,7 @@
|
||||
while (++index < length) {
|
||||
var source = sources[index];
|
||||
if (source) {
|
||||
assigner(object, source, customizer);
|
||||
assigner(object, source, index, customizer);
|
||||
}
|
||||
}
|
||||
return object;
|
||||
@@ -5105,7 +5119,7 @@
|
||||
function mergeDefaults(objValue, srcValue, key, object, source, stack) {
|
||||
if (isObject(objValue) && isObject(srcValue)) {
|
||||
stack.set(srcValue, objValue);
|
||||
baseMerge(objValue, srcValue, mergeDefaults, stack);
|
||||
baseMerge(objValue, srcValue, undefined, mergeDefaults, stack);
|
||||
}
|
||||
return objValue === undefined
|
||||
? baseClone(srcValue, undefined, undefined, key, object)
|
||||
@@ -10438,7 +10452,7 @@
|
||||
* defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
|
||||
* // => { 'a': 1, 'b': 2 }
|
||||
*/
|
||||
var assignInWith = createAssigner(function(object, source, customizer) {
|
||||
var assignInWith = createAssigner(function(object, source, srcIndex, customizer) {
|
||||
copyObjectWith(source, keysIn(source), object, customizer);
|
||||
});
|
||||
|
||||
@@ -10468,7 +10482,7 @@
|
||||
* defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
|
||||
* // => { 'a': 1, 'b': 2 }
|
||||
*/
|
||||
var assignWith = createAssigner(function(object, source, customizer) {
|
||||
var assignWith = createAssigner(function(object, source, srcIndex, customizer) {
|
||||
copyObjectWith(source, keys(source), object, customizer);
|
||||
});
|
||||
|
||||
@@ -11148,8 +11162,8 @@
|
||||
* _.merge(users, ages);
|
||||
* // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
|
||||
*/
|
||||
var merge = createAssigner(function(object, source) {
|
||||
baseMerge(object, source);
|
||||
var merge = createAssigner(function(object, source, srcIndex) {
|
||||
baseMerge(object, source, srcIndex);
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -11187,8 +11201,8 @@
|
||||
* _.mergeWith(object, other, customizer);
|
||||
* // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
|
||||
*/
|
||||
var mergeWith = createAssigner(function(object, source, customizer) {
|
||||
baseMerge(object, source, customizer);
|
||||
var mergeWith = createAssigner(function(object, source, srcIndex, customizer) {
|
||||
baseMerge(object, source, srcIndex, customizer);
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
45
test/test.js
45
test/test.js
@@ -13086,7 +13086,7 @@
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
QUnit.test('should work with a function for `object`', function(assert) {
|
||||
QUnit.test('should merge onto function `object` values', function(assert) {
|
||||
assert.expect(2);
|
||||
|
||||
function Foo() {}
|
||||
@@ -13098,7 +13098,28 @@
|
||||
assert.strictEqual(Foo.a, 1);
|
||||
});
|
||||
|
||||
QUnit.test('should work with a non-plain `object`', function(assert) {
|
||||
QUnit.test('should not merge onto nested function values', function(assert) {
|
||||
assert.expect(3);
|
||||
|
||||
var source1 = { 'a': function() {} },
|
||||
source2 = { 'a': { 'b': 1 } },
|
||||
actual = _.merge({}, source1, source2),
|
||||
expected = { 'a': { 'b': 1 } };
|
||||
|
||||
assert.deepEqual(actual, expected);
|
||||
|
||||
source1 = { 'a': function() {} };
|
||||
source2 = { 'a': { 'b': 1 } };
|
||||
|
||||
expected = { 'a': function() {} };
|
||||
expected.a.b = 1;
|
||||
|
||||
actual = _.merge(source1, source2);
|
||||
assert.strictEqual(typeof actual.a, 'function');
|
||||
assert.strictEqual(actual.a.b, 1);
|
||||
});
|
||||
|
||||
QUnit.test('should merge onto non-plain `object` values', function(assert) {
|
||||
assert.expect(2);
|
||||
|
||||
function Foo() {}
|
||||
@@ -13233,6 +13254,26 @@
|
||||
assert.deepEqual(actual, expected);
|
||||
});
|
||||
|
||||
QUnit.test('should not augment source objects', function(assert) {
|
||||
assert.expect(6);
|
||||
|
||||
var source1 = { 'a': [{ 'a': 1 }] },
|
||||
source2 = { 'a': [{ 'b': 2 }] },
|
||||
actual = _.merge({}, source1, source2);
|
||||
|
||||
assert.deepEqual(source1.a, [{ 'a': 1 }]);
|
||||
assert.deepEqual(source2.a, [{ 'b': 2 }]);
|
||||
assert.deepEqual(actual.a, [{ 'a': 1, 'b': 2 }]);
|
||||
|
||||
var source1 = { 'a': [[1, 2, 3]] },
|
||||
source2 = { 'a': [[3, 4]] },
|
||||
actual = _.merge({}, source1, source2);
|
||||
|
||||
assert.deepEqual(source1.a, [[1, 2, 3]]);
|
||||
assert.deepEqual(source2.a, [[3, 4]]);
|
||||
assert.deepEqual(actual.a, [[3, 4, 3]]);
|
||||
});
|
||||
|
||||
QUnit.test('should shallow clone array/typed-array/plain-object sources', function(assert) {
|
||||
assert.expect(1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user