8000 Add "customization callback" support to `_.assign`. · lodash/lodash@a346478 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit a346478

Browse files
committed
Add "customization callback" support to _.assign.
Former-commit-id: 5f0c7b72942ba0c40960072b11936f0683909043
1 parent fe1eb92 commit a346478

File tree

9 files changed

+393
-307
lines changed

9 files changed

+393
-307
lines changed

build/pre-compile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/** Used to minify variables embedded in compiled strings */
99
var compiledVars = [
10+
'args',
1011
'argsIndex',
1112
'argsLength',
1213
'callback',

dist/lodash.compat.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,20 @@
441441
var assignIteratorOptions = {
442442
'args': 'object, source, guard',
443443
'top':
444-
'var argsIndex = 0,\n' +
444+
'var args = arguments,\n' +
445+
' argsIndex = 0,\n' +
445446
" argsLength = typeof guard == 'number' ? 2 : arguments.length;\n" +
447+
'if (argsLength > 2) {\n' +
448+
" if (typeof args[argsLength - 2] == 'function') {\n" +
449+
' var callback = createCallback(args[--argsLength - 1], args[argsLength--], 2);\n' +
450+
" } else if (typeof args[argsLength - 1] == 'function') {\n" +
451+
' callback = args[--argsLength];\n' +
452+
' }\n' +
453+
'}\n' +
446454
'while (++argsIndex < argsLength) {\n' +
447455
' iterable = arguments[argsIndex];\n' +
448456
' if (iterable && objectTypes[typeof iterable]) {',
449-
'loop': 'result[index] = iterable[index]',
457+
'loop': 'result[index] = callback ? callback(result[index], iterable[index]) : iterable[index]',
450458
'bottom': ' }\n}'
451459
};
452460

@@ -1019,9 +1027,11 @@
10191027
/*--------------------------------------------------------------------------*/
10201028

10211029
/**
1022-
* Assigns own enumerable properties of source object(s) to the `destination`
1030+
* Assigns own enumerable properties of source object(s) to the destination
10231031
* object. Subsequent sources will overwrite propery assignments of previous
1024-
* sources.
1032+
* sources. If a `callback` function is passed, it will be executed to produce
1033+
* the assigned values. The `callback` is bound to `thisArg` and invoked with
1034+
* two arguments; (objectValue, sourceValue).
10251035
*
10261036
* @static
10271037
* @memberOf _
@@ -1030,13 +1040,21 @@
10301040
* @category Objects
10311041
* @param {Object} object The destination object.
10321042
* @param {Object} [source1, source2, ...] The source objects.
1033-
* @param- {Object} [guard] Internally used to allow working with `_.reduce`
1034-
* without using its callback's `key and `object` arguments as sources.
1043+
* @param {Function} [callback] The function to customize assigning values.
1044+
* @param {Mixed} [thisArg] The `this` binding of `callback`.
10351045
* @returns {Object} Returns the destination object.
10361046
* @example
10371047
*
10381048
* _.assign({ 'name': 'moe' }, { 'age': 40 });
10391049
* // => { 'name': 'moe', 'age': 40 }
1050+
*
1051+
* var defaults = _.partialRight(_.assign, function(a, b) {
1052+
* return typeof a == 'undefined' ? b : a;
1053+
* });
1054+
*
1055+
* var food = { 'name': 'apple' };
1056+
* defaults(food, { 'name': 'banana', 'type': 'fruit' });
1057+
* // => { 'name': 'apple', 'type': 'fruit' }
10401058
*/
10411059
var assign = createIterator(assignIteratorOptions);
10421060

@@ -1212,8 +1230,8 @@
12121230
}
12131231

12141232
/**
1215-
* Assigns own enumerable properties of source object(s) to the `destination`
1216-
* object for all `destination` properties that resolve to `null`/`undefined`.
1233+
* Assigns own enumerable properties of source object(s) to the destination
1234+
* object for all destination properties that resolve to `null`/`undefined`.
12171235
* Once a property is set, additional defaults of the same property will be
12181236
* ignored.
12191237
*
@@ -1228,9 +1246,9 @@
12281246
* @returns {Object} Returns the destination object.
12291247
* @example
12301248
*
1231-
* var iceCream = { 'flavor': 'chocolate' };
1232-
* _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
1233-
* // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
1249+
* var food = { 'name': 'apple' };
1250+
* _.defaults(food, { 'name': 'banana', 'type': 'fruit' });
1251+
* // => { 'name': 'apple', 'type': 'fruit' }
12341252
*/
12351253
var defaults = createIterator(assignIteratorOptions, {
12361254
'loop': 'if (result[index] == null) ' + assignIteratorOptions.loop
@@ -1819,7 +1837,7 @@
18191837

18201838
/**
18211839
* Recursively merges own enumerable properties of the source object(s), that
1822-
* don't resolve to `undefined`, into the `destination` object. Subsequent sources
1840+
* don't resolve to `undefined`, into the destination object. Subsequent sources
18231841
* will overwrite propery assignments of previous sources. If a `callback` function
18241842
* is passed, it will be executed to produce the merged values of the destination
18251843
* and source properties. If `callback` returns `undefined`, merging will be
@@ -1885,15 +1903,16 @@
18851903
var callback = args[3],
18861904
stackA = args[4],
18871905
stackB = args[5];
1888-
}
1889-
else {
1906+
} else {
18901907
stackA = [];
18911908
stackB = [];
18921909

18931910
// allows working with `_.reduce` and `_.reduceRight` without
18941911
// using their `callback` arguments, `index|key` and `collection`
18951912
if (typeof deepIndicator != 'number') {
18961913
length = args.length;
1914+
}
1915+
if (length > 2) {
18971916
if (typeof args[length - 2] == 'function') {
18981917
callback = createCallback(args[--length - 1], args[length--], 2);
18991918
} else if (typeof args[length - 1] == 'function') {
@@ -4293,16 +4312,14 @@
42934312
* @returns {Function} Returns the new partially applied function.
42944313
* @example
42954314
*
4296-
* _.mixin({
4297-
* 'defaultsDeep': _.partialRight(_.merge, _.defaults)
4298-
* });
4315+
* var defaultsDeep = _.partialRight(_.merge, _.defaults);
42994316
*
43004317
* var options = {
43014318
* 'variable': 'data',
43024319
* 'imports': { 'jq': $ }
43034320
* };
43044321
*
4305-
* _.defaultsDeep(options, _.templateSettings);
4322+
* defaultsDeep(options, _.templateSettings);
43064323
*
43074324
* options.variable
43084325
* // => 'data'

0 commit comments

Comments
 (0)
0