Ensure _.defaultsDeep doesn't modify sources.

This commit is contained in:
John-David Dalton
2015-10-11 01:39:30 -07:00
parent 414ad602f2
commit a08d52eb48
2 changed files with 16 additions and 3 deletions

View File

@@ -4786,10 +4786,10 @@
*/
function mergeDefaults(objValue, srcValue, key, object, source, stack) {
if (isObject(objValue)) {
stack.set(objValue, objValue);
stack.set(srcValue, objValue);
baseMerge(objValue, srcValue, mergeDefaults, stack);
}
return objValue === undefined ? srcValue : objValue;
return objValue === undefined ? baseClone(srcValue) : objValue;
}
/**

View File

@@ -3562,7 +3562,20 @@
source.bar.b = source.foo.b;
var actual = _.defaultsDeep(object, source);
assert.ok(actual.bar.b === source.foo.b && actual.foo.b.c.d === actual.foo.b.c.d.foo.b.c.d);
assert.ok(actual.bar.b === actual.foo.b && actual.foo.b.c.d === actual.foo.b.c.d.foo.b.c.d);
});
QUnit.test('should not modify sources', function(assert) {
assert.expect(3);
var object = {},
source1 = { 'a': 1, 'b': { 'c': 2 } },
source2 = { 'b': { 'c': 3, 'd': 3 } },
actual = _.defaultsDeep(object, source1, source2);
assert.deepEqual(actual, { 'a': 1, 'b': { 'c': 2, 'd': 3 } });
assert.deepEqual(source1, { 'a': 1, 'b': { 'c': 2 } });
assert.deepEqual(source2, { 'b': { 'c': 3, 'd': 3 } });
});
}());