Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure `_.merge` applies a given `callback` to nested properties. [cl…

…oses #227]
  • Loading branch information...
commit 68fcc3034b60bbecdfa9efaa02c2ac6472f24db7 1 parent 8286915
John-David Dalton jdalton authored
Showing with 14 additions and 4 deletions.
  1. +5 −2 lodash.js
  2. +9 −2 test/test.js
7 lodash.js
View
@@ -2005,6 +2005,7 @@
var callback = args[3],
stackA = args[4],
stackB = args[5];
+ console.log(object)
} else {
stackA = [];
stackB = [];
@@ -2041,9 +2042,10 @@
? (isArray(value) ? value : [])
: (isPlainObject(value) ? value : {});
+ var isShallow;
if (callback) {
result = callback(value, source);
- if (typeof result != 'undefined') {
+ if ((isShallow = typeof result != 'undefined')) {
value = result;
}
}
@@ -2052,13 +2054,14 @@
stackB.push(value);
// recursively merge objects and arrays (susceptible to call stack limits)
- if (!callback) {
+ if (!isShallow) {
value = merge(value, source, indicatorObject, callback, stackA, stackB);
}
}
}
else {
if (callback) {
+ console.log('hi')
result = callback(value, source);
if (typeof result == 'undefined') {
result = source;
11 test/test.js
View
@@ -1801,8 +1801,15 @@
});
test('should handle merging if `callback` returns `undefined`', function() {
- var actual = _.merge({ 'a': 1 }, { 'a': 2 }, function() { });
- deepEqual(actual, { 'a': 2 });
+ var actual = _.merge({ 'a': { 'b': [1, 1] } }, { 'a': { 'b': [0] } }, function() { });
+ deepEqual(actual, { 'a': { 'b': [0, 1] } });
+ });
+
+ test('should defer to `callback` when it returns a value other than `undefined`', function() {
+ var actual = _.merge({ 'a': { 'b': [0, 1] } }, { 'a': { 'b': [2] } }, function(a, b) {
+ return _.isArray(a) ? a.concat(b) : undefined;
+ });
+ deepEqual(actual, { 'a': { 'b': [0, 1, 2] } });
});
}(1, 2, 3));
Please sign in to comment.
Something went wrong with that request. Please try again.