-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathmerge.js
31 lines (29 loc) · 1007 Bytes
/
merge.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
/**
* We have relatively simple deep merging requirements in this package.
* We are only ever merging messages config, so we know the structure,
* we know there are no arrays, and we know there are no constructors
* or weirdly defined properties.
*
* Thus, we can write a very simplistic deep merge function to avoid
* pulling in a large dependency.
*/
export default function merge(destination, ...sources) {
sources.forEach((source) => {
Object.keys(source).forEach((prop) => {
if (prop === '__proto__') return; // protect against prototype pollution
if (
source[prop]
&& source[prop].constructor
&& source[prop].constructor === Object
) {
if (!destination[prop] || !destination[prop].constructor || destination[prop].constructor !== Object) {
destination[prop] = {};
}
merge(destination[prop], source[prop]);
} else {
destination[prop] = source[prop];
}
});
});
return destination;
}