Add _.updateWith fp support.

This commit is contained in:
John-David Dalton
2016-02-28 12:16:21 -08:00
parent fab50ec384
commit eabe95c4e3
2 changed files with 37 additions and 14 deletions

View File

@@ -73,7 +73,7 @@ exports.aryMethod = {
'transform', 'unionBy', 'unionWith', 'update', 'xorBy', 'xorWith', 'zipWith' 'transform', 'unionBy', 'unionWith', 'update', 'xorBy', 'xorWith', 'zipWith'
], ],
'4': [ '4': [
'fill', 'setWith' 'fill', 'setWith', 'updateWith'
] ]
}; };
@@ -121,8 +121,7 @@ exports.iterateeAry = {
'takeRightWhile': 1, 'takeRightWhile': 1,
'takeWhile': 1, 'takeWhile': 1,
'times': 1, 'times': 1,
'transform': 2, 'transform': 2
'update': 1
}; };
/** Used to map method names to iteratee rearg configs. */ /** Used to map method names to iteratee rearg configs. */
@@ -142,6 +141,7 @@ exports.methodRearg = {
'setWith': [3, 1, 2, 0], 'setWith': [3, 1, 2, 0],
'sortedIndexBy': [2, 1, 0], 'sortedIndexBy': [2, 1, 0],
'sortedLastIndexBy': [2, 1, 0], 'sortedLastIndexBy': [2, 1, 0],
'updateWith': [3, 1, 2, 0],
'zipWith': [1, 2, 0] 'zipWith': [1, 2, 0]
}; };
@@ -177,7 +177,8 @@ exports.mutate = {
'set': true, 'set': true,
'setWith': true, 'setWith': true,
'unset': true, 'unset': true,
'update': true 'update': true,
'updateWith': true
} }
}; };

View File

@@ -563,7 +563,7 @@
deepObject = { 'a': { 'b': 2, 'c': 3 } }; deepObject = { 'a': { 'b': 2, 'c': 3 } };
QUnit.test('should not mutate values', function(assert) { QUnit.test('should not mutate values', function(assert) {
assert.expect(40); assert.expect(42);
function Foo() {} function Foo() {}
Foo.prototype = { 'b': 2 }; Foo.prototype = { 'b': 2 };
@@ -697,10 +697,16 @@
assert.deepEqual(actual, { 'a': { 'c': 3 } }, 'fp.unset'); assert.deepEqual(actual, { 'a': { 'c': 3 } }, 'fp.unset');
value = _.cloneDeep(deepObject); 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(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 } }; deepObject = { 'a': { 'b': 2, 'c': 3 } };
QUnit.test('should only clone objects in `path`', function(assert) { 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 } } }, var object = { 'a': { 'b': { 'c': 1 }, 'd': { 'e': 1 } } },
value = _.cloneDeep(object), value = _.cloneDeep(object),
@@ -761,10 +767,9 @@
assert.strictEqual(actual.d, value.d, 'fp.set'); assert.strictEqual(actual.d, value.d, 'fp.set');
value = _.cloneDeep(object); 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.deepEqual(actual[0], { '1': 'a' }, 'fp.setWith');
assert.strictEqual(actual.d, value.d, 'fp.setWith');
value = _.cloneDeep(object); value = _.cloneDeep(object);
actual = fp.unset('a.b')(value); actual = fp.unset('a.b')(value);
@@ -772,9 +777,17 @@
assert.notOk('b' in actual, 'fp.unset'); assert.notOk('b' in actual, 'fp.unset');
assert.strictEqual(actual.d, value.d, 'fp.unset'); assert.strictEqual(actual.d, value.d, 'fp.unset');
value = _.cloneDeep(object); value = _.cloneDeep(deepObject);
actual = fp.update('a.b', function(x) { return { 'c2': 2 }; }, value); 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'); 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 }; object = { 'a': 1 };
QUnit.test('should provide the correct `customizer` arguments', function(assert) { QUnit.test('should provide the correct `customizer` arguments', function(assert) {
assert.expect(4); assert.expect(5);
var args, var args,
value = _.clone(object); value = _.clone(object);
@@ -828,6 +841,15 @@
})('b.c')(2)(value); })('b.c')(2)(value);
assert.deepEqual(args, [undefined, 'b', { 'a': 1 }], 'fp.setWith'); 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');
}); });
}()); }());