From eabe95c4e3c6ce4cc20b36213e58cb8b01dc6d1d Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 28 Feb 2016 12:16:21 -0800 Subject: [PATCH] Add `_.updateWith` fp support. --- fp/_mapping.js | 9 +++++---- test/test-fp.js | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/fp/_mapping.js b/fp/_mapping.js index 6226119ee..1d33d4b0a 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -73,7 +73,7 @@ exports.aryMethod = { 'transform', 'unionBy', 'unionWith', 'update', 'xorBy', 'xorWith', 'zipWith' ], '4': [ - 'fill', 'setWith' + 'fill', 'setWith', 'updateWith' ] }; @@ -121,8 +121,7 @@ exports.iterateeAry = { 'takeRightWhile': 1, 'takeWhile': 1, 'times': 1, - 'transform': 2, - 'update': 1 + 'transform': 2 }; /** Used to map method names to iteratee rearg configs. */ @@ -142,6 +141,7 @@ exports.methodRearg = { 'setWith': [3, 1, 2, 0], 'sortedIndexBy': [2, 1, 0], 'sortedLastIndexBy': [2, 1, 0], + 'updateWith': [3, 1, 2, 0], 'zipWith': [1, 2, 0] }; @@ -177,7 +177,8 @@ exports.mutate = { 'set': true, 'setWith': true, 'unset': true, - 'update': true + 'update': true, + 'updateWith': true } }; diff --git a/test/test-fp.js b/test/test-fp.js index 4ccea5041..e47edd107 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -563,7 +563,7 @@ deepObject = { 'a': { 'b': 2, 'c': 3 } }; QUnit.test('should not mutate values', function(assert) { - assert.expect(40); + assert.expect(42); function Foo() {} Foo.prototype = { 'b': 2 }; @@ -697,10 +697,16 @@ assert.deepEqual(actual, { 'a': { 'c': 3 } }, 'fp.unset'); value = _.cloneDeep(deepObject); - actual = fp.update('a.b')(function(x) { return x + 1; })(value); + actual = fp.update('a.b')(function(n) { return n * n; })(value); assert.deepEqual(value, deepObject, 'fp.update'); - assert.deepEqual(actual, { 'a': { 'b': 3, 'c': 3 } }, 'fp.update'); + assert.deepEqual(actual, { 'a': { 'b': 4, 'c': 3 } }, 'fp.update'); + + value = _.cloneDeep(deepObject); + actual = fp.updateWith(Object)('d.e')(_.constant(4))(value); + + assert.deepEqual(value, deepObject, 'fp.updateWith'); + assert.deepEqual(actual, { 'a': { 'b': 2, 'c': 3 }, 'd': { 'e': 4 } }, 'fp.updateWith'); }); }()); @@ -748,7 +754,7 @@ deepObject = { 'a': { 'b': 2, 'c': 3 } }; QUnit.test('should only clone objects in `path`', function(assert) { - assert.expect(9); + assert.expect(11); var object = { 'a': { 'b': { 'c': 1 }, 'd': { 'e': 1 } } }, value = _.cloneDeep(object), @@ -761,10 +767,9 @@ assert.strictEqual(actual.d, value.d, 'fp.set'); value = _.cloneDeep(object); - actual = fp.setWith(Object)('a.b.c')(2)(value); + actual = fp.setWith(Object)('[0][1]')('a')(value); - assert.strictEqual(actual.a.b.c, 2, 'fp.setWith'); - assert.strictEqual(actual.d, value.d, 'fp.setWith'); + assert.deepEqual(actual[0], { '1': 'a' }, 'fp.setWith'); value = _.cloneDeep(object); actual = fp.unset('a.b')(value); @@ -772,9 +777,17 @@ assert.notOk('b' in actual, 'fp.unset'); assert.strictEqual(actual.d, value.d, 'fp.unset'); - value = _.cloneDeep(object); - actual = fp.update('a.b', function(x) { return { 'c2': 2 }; }, value); + value = _.cloneDeep(deepObject); + actual = fp.update('a.b')(function(n) { return n * n; })(value); + + assert.strictEqual(actual.a.b, 4, 'fp.update'); assert.strictEqual(actual.d, value.d, 'fp.update'); + + value = _.cloneDeep(deepObject); + actual = fp.updateWith(Object)('[0][1]')(_.constant('a'))(value); + + assert.deepEqual(actual[0], { '1': 'a' }, 'fp.updateWith'); + assert.strictEqual(actual.d, value.d, 'fp.updateWith'); }); }()); @@ -787,7 +800,7 @@ object = { 'a': 1 }; QUnit.test('should provide the correct `customizer` arguments', function(assert) { - assert.expect(4); + assert.expect(5); var args, value = _.clone(object); @@ -828,6 +841,15 @@ })('b.c')(2)(value); assert.deepEqual(args, [undefined, 'b', { 'a': 1 }], 'fp.setWith'); + + args = undefined; + value = _.clone(object); + + fp.updateWith(function() { + args || (args = _.map(arguments, _.cloneDeep)); + })('b.c')(_.constant(2))(value); + + assert.deepEqual(args, [undefined, 'b', { 'a': 1 }], 'fp.updateWith'); }); }());