-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeDeep.js
More file actions
28 lines (23 loc) · 866 Bytes
/
Copy pathmergeDeep.js
File metadata and controls
28 lines (23 loc) · 866 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
const { mergeObject } = require("./reduce");
const isObject = object => typeof object === "object"
&& !Array.isArray(object)
// Merge deep between two objects with recursive call on nested values (value, array or object)
// objects => list of objects to merge (no array, just arguments like mergeDeep(o1, o2, o3))
// returns => single object with ubpadtes
const mergeDeep = (...objects) => objects.reduce((acc, obj) => {
if (!isObject(obj)) return acc;
return Object.keys(obj).map((k) => {
const pVal = acc[k];
const oVal = obj[k];
if (Array.isArray(pVal) && Array.isArray(oVal)) {
return { ...acc, [k]: Array.from(new Set([ ...pVal, ...oVal ])) };
} if (isObject(pVal) && isObject(oVal)) {
return { ...acc, [k]: mergeDeep(pVal, oVal) };
}
return { ...acc, [k]: oVal };
})
.reduce(mergeObject, {});
}, {});
module.exports = {
mergeDeep
}