mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add object deep merge utility function
This commit is contained in:
22
src/util.js
22
src/util.js
@@ -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 = {
|
module.exports = {
|
||||||
getConfig,
|
getConfig,
|
||||||
getConfigValue,
|
getConfigValue,
|
||||||
@@ -205,4 +226,5 @@ module.exports = {
|
|||||||
getImageBuffers,
|
getImageBuffers,
|
||||||
readAllChunks,
|
readAllChunks,
|
||||||
delay,
|
delay,
|
||||||
|
deepMerge,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user