@lp-labs/json-merge-patch
v0.1.0
Published
Detached finite-JSON RFC 7396 merge patches with explicit traversal budgets.
Readme
@lp-labs/json-merge-patch
Detached finite-JSON RFC 7396 merge patches with explicit traversal budgets.
Install
npm install @lp-labs/json-merge-patchNode.js 18+; standalone browser-compatible ES2022 core with DOM codec types. Zero runtime or peer dependencies. ESM and CommonJS with separate declarations. No filesystem, network or environment side effects.
Quickstart
import { mergePatch } from '@lp-labs/json-merge-patch';
console.log(
mergePatch(
{ title: 'old', tags: ['a'], details: { obsolete: true } },
{ title: 'new', tags: ['b'], details: { obsolete: null } },
),
);CommonJS consumers use const api = require('@lp-labs/json-merge-patch'). The same named API is available in both module formats. No default export.
API, behavior, errors and limits
mergePatch implements RFC7396: non-object patches including arrays/null replace the entire target; object patches merge recursively, null members delete, arrays never merge elementwise. Null cannot be assigned as an object member using merge-patch (root null is supported). Inputs are validated and deep-detached before applying; output shares no references with target/patch. Object outputs have null prototypes; all keys including proto are data. JsonValue permits finite numbers, plain/null-prototype objects, dense arrays; no undefined, cycles, symbols, getters, custom classes, or decorated arrays. JsonLimits maxDepth defaults 64 (root 0, maximum 256), maxNodes 100,000; each input and final result must independently fit. RangeError bounds; TypeError invalid JSON. O(target+patch+result) work/space with bounded recursion; no patch-generation/diff API.
All exports: JsonValue, JsonLimits, mergePatch.
Full TypeScript declarations
export type JsonValue =
| null
| boolean
| number
| string
| JsonValue[]
| {
[key: string]: JsonValue;
};
export interface JsonLimits {
maxDepth?: number;
maxNodes?: number;
}
export declare function mergePatch(target: JsonValue, patch: JsonValue, o?: JsonLimits): JsonValue;Development
npm ci --offline
npm run typecheck
npm test
npm run format:checkTypeScript ^5.9.3 and Prettier ^3.6.2 are development-only. Build emits dist/esm and dist/cjs. Tests use Node's test runner; consumer.mts and consumer.cts exercise bare package imports including negative type assertions.
Support
If this library is useful to you, consider supporting its maintenance with a donation via PayPal.
Author
Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.
