From 9c342eb4323770868448c0b3597a575820da6289 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 27 Oct 2015 16:59:44 -0700 Subject: [PATCH] Move `_.extend` and `_.extendWith` to alias and add `_.assignIn` and `_.assignInWith`. --- lodash.js | 58 ++++++++++++++++++++++++++++------------------------ test/test.js | 44 ++++++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 37 deletions(-) diff --git a/lodash.js b/lodash.js index d847ac4534..a13093000c 100644 --- a/lodash.js +++ b/lodash.js @@ -577,6 +577,18 @@ return false; } + /** + * Used by `_.defaults` to customize its `_.assignIn` use. + * + * @private + * @param {*} objValue The destination object property value. + * @param {*} srcValue The source object property value. + * @returns {*} Returns the value to assign to the destination object. + */ + function assignInDefaults(objValue, srcValue) { + return objValue === undefined ? srcValue : objValue; + } + /** * Assigns `value` to `key` of `object` if the existing value is not equivalent * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero) @@ -1033,18 +1045,6 @@ return '\\' + stringEscapes[chr]; } - /** - * Used by `_.defaults` to customize its `_.assign` use. - * - * @private - * @param {*} objValue The destination object property value. - * @param {*} srcValue The source object property value. - * @returns {*} Returns the value to assign to the destination object. - */ - function extendDefaults(objValue, srcValue) { - return objValue === undefined ? srcValue : objValue; - } - /** * Gets the index at which the first occurrence of `NaN` is found in `array`. * @@ -1429,11 +1429,11 @@ * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray` * * The chainable wrapper methods are: - * `after`, `ary`, `assign`, `assignWith`, `at`, `before`, `bind`, `bindAll`, - * `bindKey`, `chain`, `chunk`, `commit`, `compact`, `concat`, `conforms`, - * `conj`, `constant`, `countBy`, `create`, `curry`, `debounce`, `defaults`, - * `defaultsDeep`, `defer`, `delay`, `difference`, `differenceBy`, `disj`, - * `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `extend`, `extendWith`, + * `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`, `at`, + * `before`, `bind`, `bindAll`, `bindKey`, `chain`, `chunk`, `commit`, `compact`, + * `concat`, `conforms`, `conj`, `constant`, `countBy`, `create`, `curry`, + * `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`, `difference`, + * `differenceBy`, `disj`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, * `fill`, `filter`, `flatten`, `flattenDeep`, `flip`, `flow`, `flowRight`, * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, * `functions`, `groupBy`, `initial`, `intersection`, `intersectionBy`, @@ -9915,8 +9915,8 @@ * // => { 'user': 'barney', 'age': 36 } */ var defaults = rest(function(args) { - args.push(undefined, extendDefaults); - return extendWith.apply(undefined, args); + args.push(undefined, assignInDefaults); + return assignInWith.apply(undefined, args); }); /** @@ -9948,6 +9948,7 @@ * * @static * @memberOf _ + * @alias extend * @category Object * @param {Object} object The destination object. * @param {...Object} [sources] The source objects. @@ -9965,10 +9966,10 @@ * Foo.prototype.c = 3; * Bar.prototype.e = 5; * - * _.extend({ 'a': 1 }, new Foo, new Bar); + * _.assignIn({ 'a': 1 }, new Foo, new Bar); * // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 } */ - var extend = createAssigner(function(object, source) { + var assignIn = createAssigner(function(object, source) { copyObject(source, keysIn(source), object); }); @@ -9978,6 +9979,7 @@ * * @static * @memberOf _ + * @alias extendWith * @category Object * @param {Object} object The destination object. * @param {...Object} sources The source objects. @@ -9989,12 +9991,12 @@ * return _.isUndefined(objValue) ? srcValue : objValue; * } * - * var defaults = _.partialRight(_.extendWith, customizer); + * var defaults = _.partialRight(_.assignInWith, customizer); * * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); * // => { 'a': 1, 'b': 2 } */ - var extendWith = createAssigner(function(object, source, customizer) { + var assignInWith = createAssigner(function(object, source, customizer) { copyObjectWith(source, keysIn(source), object, customizer); }); @@ -11699,9 +11701,9 @@ options = otherOptions = undefined; } string = toString(string); - options = extendWith({}, otherOptions || options, settings, extendDefaults); + options = assignInWith({}, otherOptions || options, settings, assignInDefaults); - var imports = extendWith({}, options.imports, settings.imports, extendDefaults), + var imports = assignInWith({}, options.imports, settings.imports, assignInDefaults), importsKeys = keys(imports), importsValues = baseValues(imports, importsKeys); @@ -13086,6 +13088,8 @@ lodash.after = after; lodash.ary = ary; lodash.assign = assign; + lodash.assignIn = assignIn; + lodash.assignInWith = assignInWith; lodash.assignWith = assignWith; lodash.at = at; lodash.before = before; @@ -13114,8 +13118,6 @@ lodash.dropRight = dropRight; lodash.dropRightWhile = dropRightWhile; lodash.dropWhile = dropWhile; - lodash.extend = extend; - lodash.extendWith = extendWith; lodash.fill = fill; lodash.filter = filter; lodash.flatten = flatten; @@ -13217,6 +13219,8 @@ // Add aliases. lodash.each = forEach; lodash.eachRight = forEachRight; + lodash.extend = assignIn; + lodash.extendWith = assignInWith; // Add functions to `lodash.prototype`. mixin(lodash, lodash); diff --git a/test/test.js b/test/test.js index 4d345c2f9a..a17c28bd0d 100644 --- a/test/test.js +++ b/test/test.js @@ -1088,9 +1088,21 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('lodash.assign and lodash.extend'); + QUnit.module('lodash.assignIn'); - lodashStable.each(['assign', 'extend'], function(methodName) { + (function() { + QUnit.test('should be aliased', function(assert) { + assert.expect(1); + + assert.strictEqual(_.extend, _.assignIn); + }); + }()); + + /*--------------------------------------------------------------------------*/ + + QUnit.module('lodash.assign and lodash.assignIn'); + + lodashStable.each(['assign', 'assignIn'], function(methodName) { var func = _[methodName]; QUnit.test('`_.' + methodName + '` should assign properties of a source object to the destination object', function(assert) { @@ -1124,9 +1136,21 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('lodash.assignWith and lodash.extendWith'); + QUnit.module('lodash.assignInWith'); + + (function() { + QUnit.test('should be aliased', function(assert) { + assert.expect(1); + + assert.strictEqual(_.extendWith, _.assignInWith); + }); + }()); + + /*--------------------------------------------------------------------------*/ + + QUnit.module('lodash.assignWith and lodash.assignInWith'); - lodashStable.each(['assignWith', 'extendWith'], function(methodName) { + lodashStable.each(['assignWith', 'assignInWith'], function(methodName) { var func = _[methodName]; QUnit.test('`_.' + methodName + '` should work with a `customizer` callback', function(assert) { @@ -4661,7 +4685,7 @@ QUnit.module('strict mode checks'); - lodashStable.each(['assign', 'extend', 'bindAll', 'defaults'], function(methodName) { + lodashStable.each(['assign', 'assignIn', 'bindAll', 'defaults'], function(methodName) { var func = _[methodName], isBindAll = methodName == 'bindAll'; @@ -5778,7 +5802,7 @@ QUnit.module('object assignments'); - lodashStable.each(['assign', 'defaults', 'extend', 'merge'], function(methodName) { + lodashStable.each(['assign', 'assignIn', 'defaults', 'merge'], function(methodName) { var func = _[methodName], isAssign = methodName == 'assign', isDefaults = methodName == 'defaults'; @@ -5857,7 +5881,7 @@ }); }); - lodashStable.each(['assign', 'extend', 'merge'], function(methodName) { + lodashStable.each(['assign', 'assignIn', 'merge'], function(methodName) { var func = _[methodName]; QUnit.test('`_.' + methodName + '` should not treat `object` as `source`', function(assert) { @@ -5871,7 +5895,7 @@ }); }); - lodashStable.each(['assign', 'assignWith', 'defaults', 'extend', 'extendWith', 'merge', 'mergeWith'], function(methodName) { + lodashStable.each(['assign', 'assignIn', 'assignInWith', 'assignWith', 'defaults', 'merge', 'mergeWith'], function(methodName) { var func = _[methodName]; QUnit.test('`_.' + methodName + '` should not assign values that are the same as their destinations', function(assert) { @@ -5899,7 +5923,7 @@ }); }); - lodashStable.each(['assignWith', 'extendWith', 'mergeWith'], function(methodName) { + lodashStable.each(['assignWith', 'assignInWith', 'mergeWith'], function(methodName) { var func = _[methodName], isMergeWith = methodName == 'mergeWith'; @@ -21997,7 +22021,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(265); + assert.expect(267); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));