Move _.extend and _.extendWith to alias and add _.assignIn and _.assignInWith.

This commit is contained in:
John-David Dalton
2015-10-27 16:59:44 -07:00
parent 95f5b39aed
commit 9c342eb432
2 changed files with 65 additions and 37 deletions

View File

@@ -577,6 +577,18 @@
return false; 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 * 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) * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
@@ -1033,18 +1045,6 @@
return '\\' + stringEscapes[chr]; 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`. * Gets the index at which the first occurrence of `NaN` is found in `array`.
* *
@@ -1429,11 +1429,11 @@
* `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray` * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray`
* *
* The chainable wrapper methods are: * The chainable wrapper methods are:
* `after`, `ary`, `assign`, `assignWith`, `at`, `before`, `bind`, `bindAll`, * `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`, `at`,
* `bindKey`, `chain`, `chunk`, `commit`, `compact`, `concat`, `conforms`, * `before`, `bind`, `bindAll`, `bindKey`, `chain`, `chunk`, `commit`, `compact`,
* `conj`, `constant`, `countBy`, `create`, `curry`, `debounce`, `defaults`, * `concat`, `conforms`, `conj`, `constant`, `countBy`, `create`, `curry`,
* `defaultsDeep`, `defer`, `delay`, `difference`, `differenceBy`, `disj`, * `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`, `difference`,
* `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `extend`, `extendWith`, * `differenceBy`, `disj`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`,
* `fill`, `filter`, `flatten`, `flattenDeep`, `flip`, `flow`, `flowRight`, * `fill`, `filter`, `flatten`, `flattenDeep`, `flip`, `flow`, `flowRight`,
* `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
* `functions`, `groupBy`, `initial`, `intersection`, `intersectionBy`, * `functions`, `groupBy`, `initial`, `intersection`, `intersectionBy`,
@@ -9915,8 +9915,8 @@
* // => { 'user': 'barney', 'age': 36 } * // => { 'user': 'barney', 'age': 36 }
*/ */
var defaults = rest(function(args) { var defaults = rest(function(args) {
args.push(undefined, extendDefaults); args.push(undefined, assignInDefaults);
return extendWith.apply(undefined, args); return assignInWith.apply(undefined, args);
}); });
/** /**
@@ -9948,6 +9948,7 @@
* *
* @static * @static
* @memberOf _ * @memberOf _
* @alias extend
* @category Object * @category Object
* @param {Object} object The destination object. * @param {Object} object The destination object.
* @param {...Object} [sources] The source objects. * @param {...Object} [sources] The source objects.
@@ -9965,10 +9966,10 @@
* Foo.prototype.c = 3; * Foo.prototype.c = 3;
* Bar.prototype.e = 5; * 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 } * // => { '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); copyObject(source, keysIn(source), object);
}); });
@@ -9978,6 +9979,7 @@
* *
* @static * @static
* @memberOf _ * @memberOf _
* @alias extendWith
* @category Object * @category Object
* @param {Object} object The destination object. * @param {Object} object The destination object.
* @param {...Object} sources The source objects. * @param {...Object} sources The source objects.
@@ -9989,12 +9991,12 @@
* return _.isUndefined(objValue) ? srcValue : objValue; * return _.isUndefined(objValue) ? srcValue : objValue;
* } * }
* *
* var defaults = _.partialRight(_.extendWith, customizer); * var defaults = _.partialRight(_.assignInWith, customizer);
* *
* defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
* // => { 'a': 1, 'b': 2 } * // => { 'a': 1, 'b': 2 }
*/ */
var extendWith = createAssigner(function(object, source, customizer) { var assignInWith = createAssigner(function(object, source, customizer) {
copyObjectWith(source, keysIn(source), object, customizer); copyObjectWith(source, keysIn(source), object, customizer);
}); });
@@ -11699,9 +11701,9 @@
options = otherOptions = undefined; options = otherOptions = undefined;
} }
string = toString(string); 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), importsKeys = keys(imports),
importsValues = baseValues(imports, importsKeys); importsValues = baseValues(imports, importsKeys);
@@ -13086,6 +13088,8 @@
lodash.after = after; lodash.after = after;
lodash.ary = ary; lodash.ary = ary;
lodash.assign = assign; lodash.assign = assign;
lodash.assignIn = assignIn;
lodash.assignInWith = assignInWith;
lodash.assignWith = assignWith; lodash.assignWith = assignWith;
lodash.at = at; lodash.at = at;
lodash.before = before; lodash.before = before;
@@ -13114,8 +13118,6 @@
lodash.dropRight = dropRight; lodash.dropRight = dropRight;
lodash.dropRightWhile = dropRightWhile; lodash.dropRightWhile = dropRightWhile;
lodash.dropWhile = dropWhile; lodash.dropWhile = dropWhile;
lodash.extend = extend;
lodash.extendWith = extendWith;
lodash.fill = fill; lodash.fill = fill;
lodash.filter = filter; lodash.filter = filter;
lodash.flatten = flatten; lodash.flatten = flatten;
@@ -13217,6 +13219,8 @@
// Add aliases. // Add aliases.
lodash.each = forEach; lodash.each = forEach;
lodash.eachRight = forEachRight; lodash.eachRight = forEachRight;
lodash.extend = assignIn;
lodash.extendWith = assignInWith;
// Add functions to `lodash.prototype`. // Add functions to `lodash.prototype`.
mixin(lodash, lodash); mixin(lodash, lodash);

View File

@@ -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]; var func = _[methodName];
QUnit.test('`_.' + methodName + '` should assign properties of a source object to the destination object', function(assert) { 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');
lodashStable.each(['assignWith', 'extendWith'], function(methodName) { (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', 'assignInWith'], function(methodName) {
var func = _[methodName]; var func = _[methodName];
QUnit.test('`_.' + methodName + '` should work with a `customizer` callback', function(assert) { QUnit.test('`_.' + methodName + '` should work with a `customizer` callback', function(assert) {
@@ -4661,7 +4685,7 @@
QUnit.module('strict mode checks'); QUnit.module('strict mode checks');
lodashStable.each(['assign', 'extend', 'bindAll', 'defaults'], function(methodName) { lodashStable.each(['assign', 'assignIn', 'bindAll', 'defaults'], function(methodName) {
var func = _[methodName], var func = _[methodName],
isBindAll = methodName == 'bindAll'; isBindAll = methodName == 'bindAll';
@@ -5778,7 +5802,7 @@
QUnit.module('object assignments'); QUnit.module('object assignments');
lodashStable.each(['assign', 'defaults', 'extend', 'merge'], function(methodName) { lodashStable.each(['assign', 'assignIn', 'defaults', 'merge'], function(methodName) {
var func = _[methodName], var func = _[methodName],
isAssign = methodName == 'assign', isAssign = methodName == 'assign',
isDefaults = methodName == 'defaults'; isDefaults = methodName == 'defaults';
@@ -5857,7 +5881,7 @@
}); });
}); });
lodashStable.each(['assign', 'extend', 'merge'], function(methodName) { lodashStable.each(['assign', 'assignIn', 'merge'], function(methodName) {
var func = _[methodName]; var func = _[methodName];
QUnit.test('`_.' + methodName + '` should not treat `object` as `source`', function(assert) { 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]; var func = _[methodName];
QUnit.test('`_.' + methodName + '` should not assign values that are the same as their destinations', function(assert) { 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], var func = _[methodName],
isMergeWith = methodName == 'mergeWith'; isMergeWith = methodName == 'mergeWith';
@@ -21997,7 +22021,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(265); assert.expect(267);
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([])); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));