fix: prevent config clobbering
This commit is contained in:
28
src/config/merge-patch.ts
Normal file
28
src/config/merge-patch.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
type PlainObject = Record<string, unknown>;
|
||||
|
||||
function isPlainObject(value: unknown): value is PlainObject {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function applyMergePatch(base: unknown, patch: unknown): unknown {
|
||||
if (!isPlainObject(patch)) {
|
||||
return patch;
|
||||
}
|
||||
|
||||
const result: PlainObject = isPlainObject(base) ? { ...base } : {};
|
||||
|
||||
for (const [key, value] of Object.entries(patch)) {
|
||||
if (value === null) {
|
||||
delete result[key];
|
||||
continue;
|
||||
}
|
||||
if (isPlainObject(value)) {
|
||||
const baseValue = result[key];
|
||||
result[key] = applyMergePatch(isPlainObject(baseValue) ? baseValue : {}, value);
|
||||
continue;
|
||||
}
|
||||
result[key] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user