27 lines
645 B
TypeScript
27 lines
645 B
TypeScript
import { isPlainObject } from "../utils.js";
|
|
|
|
type PlainObject = Record<string, unknown>;
|
|
|
|
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;
|
|
}
|