From 01e176694f69cda8bb99260dd4cd342479332759 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 26 Jan 2016 23:41:34 -0800 Subject: [PATCH] Correct fp arg order of `assignWith`, `assignInWith`, `merge`, `mergeWith`, `defaults`, and `defaultsDeep`. --- fp/_mapping.js | 7 ++++--- test/test-fp.js | 49 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/fp/_mapping.js b/fp/_mapping.js index 9da65abc5..001b9be55 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -49,7 +49,7 @@ module.exports = { 'template', 'trim', 'trimEnd', 'trimStart', 'uniqueId', 'words' ], 2: [ - 'add', 'after', 'ary', 'assign', 'at', 'before', 'bind', 'bindKey', + 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindKey', 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN', 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'delay', 'difference', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'eq', @@ -97,7 +97,10 @@ module.exports = { /** Used to map method names to rearg configs. */ 'methodRearg': { + 'assignInWith': [1, 2, 0], + 'assignWith': [1, 2, 0], 'clamp': [2, 0, 1], + 'mergeWith': [1, 2, 0], 'reduce': [2, 0, 1], 'reduceRight': [2, 0, 1], 'set': [2, 0, 1], @@ -189,8 +192,6 @@ module.exports = { 'assign': true, 'assignIn': true, 'concat': true, - 'defaults': true, - 'defaultsDeep': true, 'difference': true, 'matchesProperty': true, 'merge': true, diff --git a/test/test-fp.js b/test/test-fp.js index aff2571d2..1f5163816 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -803,13 +803,13 @@ deepObject = { 'a': { 'b': 2, 'c': 3 } }; QUnit.test('should not mutate values', function(assert) { - assert.expect(32); + assert.expect(36); function Foo() {} Foo.prototype = { 'b': 2 }; var value = _.clone(object), - actual = fp.assign({ 'b': 2 }, value); + actual = fp.assign(value, { 'b': 2 }); assert.deepEqual(value, object, 'fp.assign'); assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.assign'); @@ -817,26 +817,40 @@ value = _.clone(object); actual = fp.assignWith(function(objValue, srcValue) { return srcValue; - }, { 'b': 2 }, value); + }, value, { 'b': 2 }); assert.deepEqual(value, object, 'fp.assignWith'); assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.assignWith'); value = _.clone(object); - actual = fp.defaults(value, { 'a': 2, 'b': 2 }); + actual = fp.assignIn(value, new Foo); + + assert.deepEqual(value, object, 'fp.assignIn'); + assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.assignIn'); + + value = _.clone(object); + actual = fp.assignInWith(function(objValue, srcValue) { + return srcValue; + }, value, new Foo); + + assert.deepEqual(value, object, 'fp.assignInWith'); + assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.assignInWith'); + + value = _.clone(object); + actual = fp.defaults({ 'a': 2, 'b': 2 }, value); assert.deepEqual(value, object, 'fp.defaults'); assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.defaults'); value = _.clone(object); value.b = { 'c': 1 }; - actual = fp.defaultsDeep(value, { 'b': { 'c': 2, 'd': 2 } }); + actual = fp.defaultsDeep({ 'b': { 'c': 2, 'd': 2 } }, value); assert.deepEqual(value, { 'a': 1, 'b': { 'c': 1 } } , 'fp.defaultsDeep'); assert.deepEqual(actual, { 'a': 1, 'b': { 'c': 1, 'd': 2 } }, 'fp.defaultsDeep'); value = _.clone(object); - actual = fp.extend(new Foo, value); + actual = fp.extend(value, new Foo); assert.deepEqual(value, object, 'fp.extend'); assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.extend'); @@ -844,7 +858,7 @@ value = _.clone(object); actual = fp.extendWith(function(objValue, srcValue) { return srcValue; - }, new Foo, value); + }, value, new Foo); assert.deepEqual(value, object, 'fp.extendWith'); assert.deepEqual(actual, { 'a': 1, 'b': 2 }, 'fp.extendWith'); @@ -856,7 +870,7 @@ assert.deepEqual(actual, [1, '*', 3], 'fp.fill'); value = { 'a': { 'b': 2 } }; - actual = fp.merge({ 'a': { 'c': 3 } }, value); + actual = fp.merge(value, { 'a': { 'c': 3 } }); assert.deepEqual(value, { 'a': { 'b': 2 } }, 'fp.merge'); assert.deepEqual(actual, { 'a': { 'b': 2, 'c': 3 } }, 'fp.merge'); @@ -866,7 +880,7 @@ if (_.isArray(objValue)) { return objValue.concat(srcValue); } - }, { 'a': [2, 3] }, value); + }, value, { 'a': [2, 3] }); assert.deepEqual(value, { 'a': [1] }, 'fp.mergeWith'); assert.deepEqual(actual, { 'a': [1, 2, 3] }, 'fp.mergeWith'); @@ -931,17 +945,18 @@ var args, value = _.clone(object); - var actual = fp.assignWith(function() { + fp.assignWith(function() { args || (args = _.map(arguments, _.cloneDeep)); - }, { 'b': 2 }, value); + }, value, { 'b': 2 }); assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.assignWith'); args = undefined; value = _.clone(object); - actual = fp.extendWith(function() { + + fp.extendWith(function() { args || (args = _.map(arguments, _.cloneDeep)); - }, { 'b': 2 }, value); + }, value, { 'b': 2 }); assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.extendWith'); @@ -950,16 +965,18 @@ args = undefined; value = { 'a': [1] }; - actual = fp.mergeWith(function() { + + fp.mergeWith(function() { args || (args = _.map(arguments, _.cloneDeep)); - }, { 'a': [2, 3] }, value); + }, value, { 'a': [2, 3] }); args[5] = _.omitBy(args[5], _.isFunction); assert.deepEqual(args, expected, 'fp.mergeWith'); args = undefined; value = _.clone(object); - actual = fp.setWith(function() { + + fp.setWith(function() { args || (args = _.map(arguments, _.cloneDeep)); }, 'b.c', 2, value);