mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Ensure _.merge merges plain-objects onto non plain-objects. [closes #1413]
This commit is contained in:
@@ -2315,7 +2315,7 @@
|
|||||||
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
else if (isPlainObject(srcValue) || isArguments(srcValue)) {
|
||||||
newValue = isArguments(oldValue)
|
newValue = isArguments(oldValue)
|
||||||
? toPlainObject(oldValue)
|
? toPlainObject(oldValue)
|
||||||
: (isPlainObject(oldValue) ? oldValue : {});
|
: (isObject(oldValue) ? oldValue : {});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
isCommon = isFunction(srcValue);
|
isCommon = isFunction(srcValue);
|
||||||
|
|||||||
49
test/test.js
49
test/test.js
@@ -5141,29 +5141,29 @@
|
|||||||
var args,
|
var args,
|
||||||
object = { 'a': 1 },
|
object = { 'a': 1 },
|
||||||
source = { 'a': 2 },
|
source = { 'a': 2 },
|
||||||
expected = [1, 2, 'a', object, source];
|
expected = _.map([1, 2, 'a', object, source], _.clone);
|
||||||
|
|
||||||
func(object, source, function() {
|
|
||||||
args || (args = slice.call(arguments));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (isMergeWith) {
|
if (isMergeWith) {
|
||||||
expected.push(undefined, undefined);
|
expected.push(undefined, undefined);
|
||||||
}
|
}
|
||||||
|
func(object, source, function() {
|
||||||
|
args || (args = _.map(arguments, _.clone));
|
||||||
|
});
|
||||||
|
|
||||||
deepEqual(args, expected, 'primitive property values');
|
deepEqual(args, expected, 'primitive property values');
|
||||||
|
|
||||||
args = null;
|
args = null;
|
||||||
object = { 'a': 1 };
|
object = { 'a': 1 };
|
||||||
source = { 'b': 2 };
|
source = { 'b': 2 };
|
||||||
|
expected = _.map([undefined, 2, 'b', object, source], _.clone);
|
||||||
|
|
||||||
func(object, source, function() {
|
|
||||||
args || (args = slice.call(arguments));
|
|
||||||
});
|
|
||||||
|
|
||||||
expected = [undefined, 2, 'b', object, source];
|
|
||||||
if (isMergeWith) {
|
if (isMergeWith) {
|
||||||
expected.push(undefined, undefined);
|
expected.push(undefined, undefined);
|
||||||
}
|
}
|
||||||
|
func(object, source, function() {
|
||||||
|
args || (args = _.map(arguments, _.clone));
|
||||||
|
});
|
||||||
|
|
||||||
deepEqual(args, expected, 'missing destination property');
|
deepEqual(args, expected, 'missing destination property');
|
||||||
|
|
||||||
var argsList = [],
|
var argsList = [],
|
||||||
@@ -5172,16 +5172,16 @@
|
|||||||
|
|
||||||
object = { 'a': objectValue };
|
object = { 'a': objectValue };
|
||||||
source = { 'a': sourceValue };
|
source = { 'a': sourceValue };
|
||||||
|
expected = [_.map([objectValue, sourceValue, 'a', object, source], _.cloneDeep)];
|
||||||
|
|
||||||
|
if (isMergeWith) {
|
||||||
|
expected[0].push([], []);
|
||||||
|
expected.push(_.map([undefined, 2, 'b', objectValue, sourceValue, [sourceValue], [objectValue]], _.cloneDeep));
|
||||||
|
}
|
||||||
func(object, source, function() {
|
func(object, source, function() {
|
||||||
argsList.push(slice.call(arguments));
|
argsList.push(_.map(arguments, _.cloneDeep));
|
||||||
});
|
});
|
||||||
|
|
||||||
expected = [[objectValue, sourceValue, 'a', object, source]];
|
|
||||||
if (isMergeWith) {
|
|
||||||
expected[0].push([sourceValue], [sourceValue]);
|
|
||||||
expected.push([undefined, 2, 'b', sourceValue, sourceValue, [sourceValue], [sourceValue]]);
|
|
||||||
}
|
|
||||||
deepEqual(argsList, expected, 'object property values');
|
deepEqual(argsList, expected, 'object property values');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -10352,6 +10352,23 @@
|
|||||||
deepEqual(actual, expected);
|
deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should merge plain-objects onto non plain-objects', 4, function() {
|
||||||
|
function Foo(object) {
|
||||||
|
_.assign(this, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
var object = { 'a': 1 },
|
||||||
|
actual = _.merge(new Foo, object);
|
||||||
|
|
||||||
|
ok(actual instanceof Foo);
|
||||||
|
deepEqual(actual, new Foo(object));
|
||||||
|
|
||||||
|
actual = _.merge([new Foo], [object]);
|
||||||
|
|
||||||
|
ok(actual[0] instanceof Foo);
|
||||||
|
deepEqual(actual, [new Foo(object)]);
|
||||||
|
});
|
||||||
|
|
||||||
test('should convert values to arrays when merging with arrays of `source`', 2, function() {
|
test('should convert values to arrays when merging with arrays of `source`', 2, function() {
|
||||||
var object = { 'a': { '1': 'y', 'b': 'z', 'length': 2 } },
|
var object = { 'a': { '1': 'y', 'b': 'z', 'length': 2 } },
|
||||||
actual = _.merge(object, { 'a': ['x'] });
|
actual = _.merge(object, { 'a': ['x'] });
|
||||||
|
|||||||
Reference in New Issue
Block a user