json-patch-lite
v0.1.0
Published
Minimal RFC 6902 JSON Patch implementation — apply and generate add/remove/replace/move/copy/test operations against plain JS objects.
Maintainers
Readme
json-patch-lite
Minimal RFC 6902 JSON Patch: apply add/remove/replace/move/copy/test operations, and generate a patch between two plain objects. Zero dependencies.
Install
npm install json-patch-liteQuick start
import { applyPatch, generatePatch } from 'json-patch-lite';
const doc = { name: 'Tejas', role: 'engineer' };
const patched = applyPatch(doc, [{ op: 'replace', path: '/role', value: 'architect' }]);
const patch = generatePatch(oldConfig, newConfig);
// send `patch` over the wire instead of the whole documentWhy this exists
Sending full documents on every update wastes bandwidth and makes it hard to reason about what actually changed. JSON Patch (RFC 6902) is the standard, minimal wire format for "here's exactly what changed" — this package covers applying patches and generating them from a before/after pair, without pulling in a general-purpose diff library.
API
applyPatch(doc, patch)— returns a new document with the patch applied (does not mutatedoc)generatePatch(from, to)— returns the minimal add/remove/replace ops turningfromintoto
generatePatch does object-tree diffing only (no move/copy detection or array-element diffing) — arrays that differ are replaced wholesale, which keeps the diff logic small and predictable.
License
MIT
