Skip to content

Commit

Permalink
Move _.extend and _.extendWith to alias and add _.assignIn and …
Browse files Browse the repository at this point in the history
…`_.assignInWith`.
  • Loading branch information
jdalton committed Oct 27, 2015
1 parent 95f5b39 commit 9c342eb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
58 changes: 31 additions & 27 deletions lodash.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
*
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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);
});

/**
Expand Down Expand Up @@ -9948,6 +9948,7 @@
*
* @static
* @memberOf _
* @alias extend
* @category Object
* @param {Object} object The destination object.
* @param {...Object} [sources] The source objects.
Expand All @@ -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);
});

Expand All @@ -9978,6 +9979,7 @@
*
* @static
* @memberOf _
* @alias extendWith
* @category Object
* @param {Object} object The destination object.
* @param {...Object} sources The source objects.
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 34 additions & 10 deletions test/test.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -5899,7 +5923,7 @@
});
});

lodashStable.each(['assignWith', 'extendWith', 'mergeWith'], function(methodName) {
lodashStable.each(['assignWith', 'assignInWith', 'mergeWith'], function(methodName) {
var func = _[methodName],
isMergeWith = methodName == 'mergeWith';

Expand Down Expand Up @@ -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([]));

Expand Down

0 comments on commit 9c342eb

Please sign in to comment.