-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
57 lines (49 loc) · 1.13 KB
/
index.js
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
54
55
56
57
/**
* Module dependencies.
*/
var has = Object.prototype.hasOwnProperty;
try {
var type = require('type-component');
var isEmpty = require('is-empty');
} catch (err) {
var type = require('type');
var isEmpty = require('is-empty');
}
/**
* Expose merge
*/
module.exports = merge;
/**
* Merge `b` into `a`.
*
* @param {Object} a
* @param {Object} b
* @param {Object} opts
* @return {Object} a
* @api public
*/
function merge (a, b, opts){
opts = (Boolean == typeof(opts))
? { inheritance: opts }
: opts || { inheritance: false , shallow: false, discardEmpty: true }
for (var key in b) {
var copy = !!opts.inheritance
? b[key] != null
: (has.call(b, key)) && b[key] != null
if (copy) {
if (!a) a = {};
if (!opts.shallow && 'object' === type(b[key])) {
if (!opts.discardEmpty && isEmpty(a[key]) && isEmpty(b[key])) {
// Preserve { }, null, undefined, 0 as they were
a[key] = b[key];
} else {
// merge recursively
a[key] = merge(a[key], b[key], opts);
}
} else {
a[key] = b[key];
}
}
}
return a;
};