29 lines
744 B
TypeScript
29 lines
744 B
TypeScript
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;
|
|
}
|