Add "customization callback" support to _.assign.

Former-commit-id: 5f0c7b72942ba0c40960072b11936f0683909043
This commit is contained in:
John-David Dalton
2013-02-13 01:17:51 -08:00
parent fe1eb92196
commit a3464780a1
9 changed files with 393 additions and 307 deletions

53
dist/lodash.compat.js vendored
View File

@@ -441,12 +441,20 @@
var assignIteratorOptions = {
'args': 'object, source, guard',
'top':
'var argsIndex = 0,\n' +
'var args = arguments,\n' +
' argsIndex = 0,\n' +
" argsLength = typeof guard == 'number' ? 2 : arguments.length;\n" +
'if (argsLength > 2) {\n' +
" if (typeof args[argsLength - 2] == 'function') {\n" +
' var callback = createCallback(args[--argsLength - 1], args[argsLength--], 2);\n' +
" } else if (typeof args[argsLength - 1] == 'function') {\n" +
' callback = args[--argsLength];\n' +
' }\n' +
'}\n' +
'while (++argsIndex < argsLength) {\n' +
' iterable = arguments[argsIndex];\n' +
' if (iterable && objectTypes[typeof iterable]) {',
'loop': 'result[index] = iterable[index]',
'loop': 'result[index] = callback ? callback(result[index], iterable[index]) : iterable[index]',
'bottom': ' }\n}'
};
@@ -1019,9 +1027,11 @@
/*--------------------------------------------------------------------------*/
/**
* Assigns own enumerable properties of source object(s) to the `destination`
* Assigns own enumerable properties of source object(s) to the destination
* object. Subsequent sources will overwrite propery assignments of previous
* sources.
* sources. If a `callback` function is passed, it will be executed to produce
* the assigned values. The `callback` is bound to `thisArg` and invoked with
* two arguments; (objectValue, sourceValue).
*
* @static
* @memberOf _
@@ -1030,13 +1040,21 @@
* @category Objects
* @param {Object} object The destination object.
* @param {Object} [source1, source2, ...] The source objects.
* @param- {Object} [guard] Internally used to allow working with `_.reduce`
* without using its callback's `key and `object` arguments as sources.
* @param {Function} [callback] The function to customize assigning values.
* @param {Mixed} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns the destination object.
* @example
*
* _.assign({ 'name': 'moe' }, { 'age': 40 });
* // => { 'name': 'moe', 'age': 40 }
*
* var defaults = _.partialRight(_.assign, function(a, b) {
* return typeof a == 'undefined' ? b : a;
* });
*
* var food = { 'name': 'apple' };
* defaults(food, { 'name': 'banana', 'type': 'fruit' });
* // => { 'name': 'apple', 'type': 'fruit' }
*/
var assign = createIterator(assignIteratorOptions);
@@ -1212,8 +1230,8 @@
}
/**
* Assigns own enumerable properties of source object(s) to the `destination`
* object for all `destination` properties that resolve to `null`/`undefined`.
* Assigns own enumerable properties of source object(s) to the destination
* object for all destination properties that resolve to `null`/`undefined`.
* Once a property is set, additional defaults of the same property will be
* ignored.
*
@@ -1228,9 +1246,9 @@
* @returns {Object} Returns the destination object.
* @example
*
* var iceCream = { 'flavor': 'chocolate' };
* _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
* // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
* var food = { 'name': 'apple' };
* _.defaults(food, { 'name': 'banana', 'type': 'fruit' });
* // => { 'name': 'apple', 'type': 'fruit' }
*/
var defaults = createIterator(assignIteratorOptions, {
'loop': 'if (result[index] == null) ' + assignIteratorOptions.loop
@@ -1819,7 +1837,7 @@
/**
* Recursively merges own enumerable properties of the source object(s), that
* don't resolve to `undefined`, into the `destination` object. Subsequent sources
* don't resolve to `undefined`, into the destination object. Subsequent sources
* will overwrite propery assignments of previous sources. If a `callback` function
* is passed, it will be executed to produce the merged values of the destination
* and source properties. If `callback` returns `undefined`, merging will be
@@ -1885,8 +1903,7 @@
var callback = args[3],
stackA = args[4],
stackB = args[5];
}
else {
} else {
stackA = [];
stackB = [];
@@ -1894,6 +1911,8 @@
// using their `callback` arguments, `index|key` and `collection`
if (typeof deepIndicator != 'number') {
length = args.length;
}
if (length > 2) {
if (typeof args[length - 2] == 'function') {
callback = createCallback(args[--length - 1], args[length--], 2);
} else if (typeof args[length - 1] == 'function') {
@@ -4293,16 +4312,14 @@
* @returns {Function} Returns the new partially applied function.
* @example
*
* _.mixin({
* 'defaultsDeep': _.partialRight(_.merge, _.defaults)
* });
* var defaultsDeep = _.partialRight(_.merge, _.defaults);
*
* var options = {
* 'variable': 'data',
* 'imports': { 'jq': $ }
* };
*
* _.defaultsDeep(options, _.templateSettings);
* defaultsDeep(options, _.templateSettings);
*
* options.variable
* // => 'data'