Add object deep merge utility function

This commit is contained in:
artisticMink 2023-10-25 17:19:24 +02:00
parent 91d54026a0
commit a5dc2184f5

View File

@ -196,6 +196,27 @@ async function readAllChunks(readableStream) {
});
}
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
function deepMerge(target, source) {
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!(key in target))
Object.assign(output, { [key]: source[key] });
else
output[key] = deepMerge(target[key], source[key]);
} else {
Object.assign(output, { [key]: source[key] });
}
});
}
return output;
}
module.exports = {
getConfig,
getConfigValue,
@ -205,4 +226,5 @@ module.exports = {
getImageBuffers,
readAllChunks,
delay,
deepMerge,
};