diff --git a/fp/_mapping.js b/fp/_mapping.js index a5de51640..e31a20313 100644 --- a/fp/_mapping.js +++ b/fp/_mapping.js @@ -70,7 +70,7 @@ exports.aryMethod = { 'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'isEqualWith', 'isMatchWith', 'mergeWith', 'orderBy', 'pullAllBy', 'pullAllWith', 'reduce', 'reduceRight', 'replace', 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', - 'transform', 'unionBy', 'unionWith', 'xorBy', 'xorWith', 'zipWith' + 'transform', 'unionBy', 'unionWith', 'update', 'xorBy', 'xorWith', 'zipWith' ], '4': [ 'fill', 'setWith' @@ -121,7 +121,8 @@ exports.iterateeAry = { 'takeRightWhile': 1, 'takeWhile': 1, 'times': 1, - 'transform': 2 + 'transform': 2, + 'update': 1 }; /** Used to map method names to iteratee rearg configs. */ @@ -141,6 +142,7 @@ exports.methodRearg = { 'setWith': [3, 1, 2, 0], 'sortedIndexBy': [2, 1, 0], 'sortedLastIndexBy': [2, 1, 0], + 'update': [2, 1, 0], 'zipWith': [1, 2, 0] }; @@ -175,7 +177,8 @@ exports.mutate = { 'set': { 'set': true, 'setWith': true, - 'unset': true + 'unset': true, + 'update': true } }; diff --git a/lodash.js b/lodash.js index 43f2d5d88..67227155c 100644 --- a/lodash.js +++ b/lodash.js @@ -3449,6 +3449,19 @@ return object; } + /** + * The base implementation of `_.update`. + * + * @private + * @param {Object} object The object to query. + * @param {Array|string} path The path of the property to update. + * @param {Function} updater The function to produce the updated value. + * @returns {Object} Returns `object`. + */ + function baseUpdate(object, path, updater) { + return baseSet(object, path, updater(baseGet(object, path))); + } + /** * The base implementation of `setData` without support for hot loop detection. * @@ -11900,6 +11913,37 @@ return object == null ? object : baseSet(object, path, value); } + /** + * Updates the value at `path` of `object` with given `func`. If a portion + * of `path` doesn't exist it's created. Arrays are created for missing index + * properties while objects are created for all other missing properties. + * The `func` is invoked with `value` at `path` of `object`: (value). + * + * @static + * @memberOf _ + * @category Object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {Function} updater The function to produce the updated value. + * @returns {Object} Returns the updated `object`. + * @example + * + * var object = { 'a': [{ 'b': { 'c': 3, 'd': '11' } }] }; + * + * _.update(object, 'a[0].b.c', function(n) { return n + 10; }); + * console.log(object.a[0].b.c); + * // => 13 + * + * _.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; }); + * console.log(object.x[0].y.z); + * // => 0 + */ + function update(object, path, updater) { + return object == null + ? object + : baseUpdate(object, path, baseCastFunction(updater)); + } + /** * This method is like `_.set` except that it accepts `customizer` which is * invoked to produce the objects of `path`. If `customizer` returns `undefined` @@ -14555,6 +14599,7 @@ lodash.unset = unset; lodash.unzip = unzip; lodash.unzipWith = unzipWith; + lodash.update = update; lodash.values = values; lodash.valuesIn = valuesIn; lodash.without = without; diff --git a/test/test-fp.js b/test/test-fp.js index 6d0f86074..5ee2b11ae 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(38); + assert.expect(40); function Foo() {} Foo.prototype = { 'b': 2 }; @@ -695,6 +695,12 @@ assert.deepEqual(value, deepObject, 'fp.unset'); assert.deepEqual(actual, { 'a': { 'c': 3 } }, 'fp.unset'); + + value = _.cloneDeep(deepObject); + actual = fp.update(function(x) { return x + 1; })('a.b')(value); + + assert.deepEqual(value, deepObject, 'fp.update'); + assert.deepEqual(actual, { 'a': { 'b': 3, 'c': 3 } }, 'fp.update'); }); }()); @@ -742,7 +748,7 @@ deepObject = { 'a': { 'b': 2, 'c': 3 } }; QUnit.test('should only clone objects in `path`', function(assert) { - assert.expect(8); + assert.expect(9); var object = { 'a': { 'b': { 'c': 1 }, 'd': { 'e': 1 } } }, value = _.cloneDeep(object), @@ -765,6 +771,10 @@ assert.notOk('b' in actual, 'fp.unset'); assert.strictEqual(actual.d, value.d, 'fp.unset'); + + value = _.cloneDeep(object); + actual = fp.update(function(x) { return { 'c2': 2 }; }, 'a.b', value); + assert.strictEqual(actual.d, value.d, 'fp.update'); }); }()); diff --git a/test/test.js b/test/test.js index 2821b636d..6343a80f5 100644 --- a/test/test.js +++ b/test/test.js @@ -18439,6 +18439,25 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.update'); + + (function() { + QUnit.test('should call `func` with existing value on `path` of `object` and update it', function(assert) { + assert.expect(2); + + var object = { 'a': [{ 'b': { 'c': 10 } }] }; + + _.update(object, ['a', 0, 'b', 'c'], function(value) { + assert.equal(value, 10); + return 20; + }); + + assert.equal(object.a[0].b.c, 20); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('set methods'); lodashStable.each(['set', 'setWith'], function(methodName) { @@ -24389,7 +24408,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(298); + assert.expect(299); var emptyArrays = lodashStable.map(falsey, alwaysEmptyArray);