-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsealedMerge.js
More file actions
22 lines (20 loc) · 821 Bytes
/
Copy pathsealedMerge.js
File metadata and controls
22 lines (20 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function sealedMerge(base, customization) {
const merged = {};
for (const key of Object.keys(base)) {
const customizedValue = customization[key];
const isValueCustomized = key in customization;
const baseValue = base[key];
const isSameType = isValueCustomized && Object.getPrototypeOf(baseValue) === Object.getPrototypeOf(customizedValue);
if (!isValueCustomized || !isSameType) {
merged[key] = baseValue;
} else if (isSameType) {
if (Array.isArray(customizedValue) || typeof customizedValue !== "object") {
merged[key] = customizedValue;
} else {
merged[key] = sealedMerge(baseValue, customizedValue);
}
}
}
return merged;
}
module.exports.sealedMerge = sealedMerge;