@vielzeug/patchwork
v26.11.33
Published
RFC 6902 JSON Patch: structural diff, immutable apply, and guarded tryApply for sync protocols.
Readme
@vielzeug/patchwork
RFC 6902 JSON Patch: structural diff, immutable apply, and guarded tryApply for sync protocols.
Installation
pnpm add @vielzeug/patchwork
npm install @vielzeug/patchwork
yarn add @vielzeug/patchworkQuick Start
import { apply, diff, tryApply } from '@vielzeug/patchwork';
const doc = { items: ['a'], title: 'Cart' };
const patch = diff(doc, { items: ['a', 'b'], title: 'Wagen' });
// [{ op: 'add', path: '/items/-', value: 'b' }, { op: 'replace', path: '/title', value: 'Wagen' }]
const next = apply(doc, patch); // doc untouched; untouched subtrees keep their identity
tryApply(doc, [{ op: 'test', path: '/title', value: 'Other' }]);
// { ok: false, error: { index: 0, path: '/title', reason: 'test', message: … } }Zero runtime dependencies. apply is copy-on-write, so memoized selectors recompute only
over the branches a patch actually touched; tryApply turns a failed test into data you
can log and reply to, which is what conflict handling in sync protocols needs. The optional
@vielzeug/patchwork/merge subpath adds three-way, field-level reconciliation
(merge(base, local, remote) → { doc, conflicts }) while the core entry point stays
RFC 6902-only.
Documentation
License
MIT © Helmuth Saatkamp: part of the Vielzeug monorepo.
