|
441 | 441 | var assignIteratorOptions = {
|
442 | 442 | 'args': 'object, source, guard',
|
443 | 443 | 'top':
|
444 |
| - 'var argsIndex = 0,\n' + |
| 444 | + 'var args = arguments,\n' + |
| 445 | + ' argsIndex = 0,\n' + |
445 | 446 | " 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' + |
446 | 454 | 'while (++argsIndex < argsLength) {\n' +
|
447 | 455 | ' iterable = arguments[argsIndex];\n' +
|
448 | 456 | ' if (iterable && objectTypes[typeof iterable]) {',
|
449 |
| - 'loop': 'result[index] = iterable[index]', |
| 457 | + 'loop': 'result[index] = callback ? callback(result[index], iterable[index]) : iterable[index]', |
450 | 458 | 'bottom': ' }\n}'
|
451 | 459 | };
|
452 | 460 |
|
|
1019 | 1027 | /*--------------------------------------------------------------------------*/
|
1020 | 1028 |
|
1021 | 1029 | /**
|
1022 |
| - * Assigns own enumerable properties of source object(s) to the `destination` |
| 1030 | + * Assigns own enumerable properties of source object(s) to the destination |
1023 | 1031 | * 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). |
1025 | 1035 | *
|
1026 | 1036 | * @static
|
1027 | 1037 | * @memberOf _
|
|
1030 | 1040 | * @category Objects
|
1031 | 1041 | * @param {Object} object The destination object.
|
1032 | 1042 | * @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`. |
1035 | 1045 | * @returns {Object} Returns the destination object.
|
1036 | 1046 | * @example
|
1037 | 1047 | *
|
1038 | 1048 | * _.assign({ 'name': 'moe' }, { 'age': 40 });
|
1039 | 1049 | * // => { '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' } |
1040 | 1058 | */
|
1041 | 1059 | var assign = createIterator(assignIteratorOptions);
|
1042 | 1060 |
|
|
1212 | 1230 | }
|
1213 | 1231 |
|
1214 | 1232 | /**
|
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`. |
1217 | 1235 | * Once a property is set, additional defaults of the same property will be
|
1218 | 1236 | * ignored.
|
1219 | 1237 | *
|
|
1228 | 1246 | * @returns {Object} Returns the destination object.
|
1229 | 1247 | * @example
|
1230 | 1248 | *
|
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' } |
1234 | 1252 | */
|
1235 | 1253 | var defaults = createIterator(assignIteratorOptions, {
|
1236 | 1254 | 'loop': 'if (result[index] == null) ' + assignIteratorOptions.loop
|
|
1819 | 1837 |
|
1820 | 1838 | /**
|
1821 | 1839 | * 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 |
1823 | 1841 | * will overwrite propery assignments of previous sources. If a `callback` function
|
1824 | 1842 | * is passed, it will be executed to produce the merged values of the destination
|
1825 | 1843 | * and source properties. If `callback` returns `undefined`, merging will be
|
|
1885 | 1903 | var callback = args[3],
|
1886 | 1904 | stackA = args[4],
|
1887 | 1905 | stackB = args[5];
|
1888 |
| - } |
1889 |
| - else { |
| 1906 | + } else { |
1890 | 1907 | stackA = [];
|
1891 | 1908 | stackB = [];
|
1892 | 1909 |
|
1893 | 1910 | // allows working with `_.reduce` and `_.reduceRight` without
|
1894 | 1911 | // using their `callback` arguments, `index|key` and `collection`
|
1895 | 1912 | if (typeof deepIndicator != 'number') {
|
1896 | 1913 | length = args.length;
|
| 1914 | + } |
| 1915 | + if (length > 2) { |
1897 | 1916 | if (typeof args[length - 2] == 'function') {
|
1898 | 1917 | callback = createCallback(args[--length - 1], args[length--], 2);
|
1899 | 1918 | } else if (typeof args[length - 1] == 'function') {
|
|
4293 | 4312 | * @returns {Function} Returns the new partially applied function.
|
4294 | 4313 | * @example
|
4295 | 4314 | *
|
4296 |
| - * _.mixin({ |
4297 |
| - * 'defaultsDeep': _.partialRight(_.merge, _.defaults) |
4298 |
| - * }); |
| 4315 | + * var defaultsDeep = _.partialRight(_.merge, _.defaults); |
4299 | 4316 | *
|
4300 | 4317 | * var options = {
|
4301 | 4318 | * 'variable': 'data',
|
4302 | 4319 | * 'imports': { 'jq': $ }
|
4303 | 4320 | * };
|
4304 | 4321 | *
|
4305 |
| - * _.defaultsDeep(options, _.templateSettings); |
| 4322 | + * defaultsDeep(options, _.templateSettings); |
4306 | 4323 | *
|
4307 | 4324 | * options.variable
|
4308 | 4325 | * // => 'data'
|
|
0 commit comments