@winglet/json
Version:
TypeScript library for safe and efficient JSON data manipulation with RFC 6901 (JSON Pointer) and RFC 6902 (JSON Patch) compliance, featuring prototype pollution protection and immutable operations
20 lines (17 loc) • 661 B
JavaScript
import { isPlainObject } from '@winglet/common-utils/filter';
import { cloneLite } from '@winglet/common-utils/object';
import { mergePatchRecursive } from './mergePatchRecursive.mjs';
const mergePatch = (source, mergePatchBody, immutable = true) => {
if (mergePatchBody === undefined)
return source;
if (!isPlainObject(mergePatchBody))
return mergePatchBody;
const target = isPlainObject(source)
? immutable
? cloneLite(source)
: source
: {};
const patch = immutable ? cloneLite(mergePatchBody) : mergePatchBody;
return mergePatchRecursive(target, patch);
};
export { mergePatch };