forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeep-merge.js
More file actions
31 lines (27 loc) · 790 Bytes
/
deep-merge.js
File metadata and controls
31 lines (27 loc) · 790 Bytes
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
/**
* Deeply merge two objects into a new object without changing any of the source objects.
* @see https://medium.com/javascript-in-plain-english/how-to-merge-objects-in-javascript-98f2209710e3
* @param {...Object} sources
* @return {Object}
*/
function deepMerge(...sources) {
const target = {};
sources.forEach(source => {
if (!source || typeof source !== 'object' || Array.isArray(source)) {
return;
}
for (const key of Object.keys(source)) {
if (
!target.hasOwnProperty(key) ||
typeof source[key] !== 'object' ||
Array.isArray(target[key])
) {
target[key] = source[key];
} else {
target[key] = deepMerge(target[key], source[key]);
}
}
});
return target;
}
export default deepMerge;