merge-defaults.js
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var _ = require('lodash');
/**
* defaultsDeep
*
* Implement a deep version of `_.defaults`.
*
* This method is hopefully temporary, until lodash has something
* similar that can be called in a single method. For now, it's
* worth it to use a temporary module for readability.
* (i.e. I know what `_.defaults` means offhand- not true for `_.partialRight`)
*/
// In case the end user decided to do `_.defaults = require('merge-defaults')`,
// before doing anything else, let's make SURE we have a reference to the original
// `_.defaults()` method definition.
var origLodashDefaults = _.defaults;
// Corrected: see https://github.com/lodash/lodash/issues/540
// module.exports = function (/* ... */) {
// if (typeof arguments[0] !== 'object') return origLodashDefaults.apply(_, Array.prototype.slice.call(arguments));
// else {
// var result = _mergeDefaults.apply(_, Array.prototype.slice.call(arguments));
// // Ensure that original object is mutated
// _.merge(arguments[0], result);
// return result;
// }
// };
module.exports = _.partialRight(_.merge, function recursiveDefaults( /* ... */) {
// Ensure dates and arrays are not recursively merged
if (_.isArray(arguments[0]) || _.isDate(arguments[0])) {
return arguments[0];
}
return _.merge(arguments[0], arguments[1], recursiveDefaults);
});
//origLodashDefaults.apply(_, Array.prototype.slice.call(arguments));
// module.exports = _.partialRight(_.merge, _.defaults);
// module.exports = _.partialRight(_.merge, function deep(a, b) {
// // Ensure dates and arrays are not recursively merged
// if (_.isArray(a) || _.isDate(a)) {
// return a;
// }
// else return _.merge(a, b, deep);
// });