merge.js
1.01 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
var hasOwn = require('./hasOwn');
var deepClone = require('../lang/deepClone');
var isObject = require('../lang/isObject');
/**
* Deep merge objects.
*/
function merge() {
var i = 1,
key, val, obj, target;
// make sure we don't modify source element and it's properties
// objects are passed by reference
target = deepClone( arguments[0] );
while (obj = arguments[i++]) {
for (key in obj) {
if ( ! hasOwn(obj, key) ) {
continue;
}
val = obj[key];
if ( isObject(val) && isObject(target[key]) ){
// inception, deep merge objects
target[key] = merge(target[key], val);
} else {
// make sure arrays, regexp, date, objects are cloned
target[key] = deepClone(val);
}
}
}
return target;
}
module.exports = merge;