Ensure _.defaultsDeep doesn't attempt a merge of a string into an array. [closes #1560]

This commit is contained in:
John-David Dalton
2015-10-24 15:15:06 -07:00
parent 182b29662d
commit 89279009d8
2 changed files with 10 additions and 4 deletions

View File

@@ -4841,7 +4841,7 @@
* @returns {*} Returns the value to assign to the destination object. * @returns {*} Returns the value to assign to the destination object.
*/ */
function mergeDefaults(objValue, srcValue, key, object, source, stack) { function mergeDefaults(objValue, srcValue, key, object, source, stack) {
if (isObject(objValue)) { if (isObject(objValue) && isObject(srcValue)) {
stack.set(srcValue, objValue); stack.set(srcValue, objValue);
baseMerge(objValue, srcValue, mergeDefaults, stack); baseMerge(objValue, srcValue, mergeDefaults, stack);
} }

View File

@@ -3658,15 +3658,21 @@
QUnit.test('should not modify sources', function(assert) { QUnit.test('should not modify sources', function(assert) {
assert.expect(3); assert.expect(3);
var object = {}, var source1 = { 'a': 1, 'b': { 'c': 2 } },
source1 = { 'a': 1, 'b': { 'c': 2 } },
source2 = { 'b': { 'c': 3, 'd': 3 } }, source2 = { 'b': { 'c': 3, 'd': 3 } },
actual = _.defaultsDeep(object, source1, source2); actual = _.defaultsDeep({}, source1, source2);
assert.deepEqual(actual, { 'a': 1, 'b': { 'c': 2, 'd': 3 } }); assert.deepEqual(actual, { 'a': 1, 'b': { 'c': 2, 'd': 3 } });
assert.deepEqual(source1, { 'a': 1, 'b': { 'c': 2 } }); assert.deepEqual(source1, { 'a': 1, 'b': { 'c': 2 } });
assert.deepEqual(source2, { 'b': { 'c': 3, 'd': 3 } }); assert.deepEqual(source2, { 'b': { 'c': 3, 'd': 3 } });
}); });
QUnit.test('should not attempt a merge of a string into an array', function(assert) {
assert.expect(1);
var actual = _.defaultsDeep({ 'a': ['abc'] }, { 'a': 'abc' });
assert.deepEqual(actual, { 'a': ['abc'] });
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/