Skip to content

Patching

Generate RFC 6902 JSON Patch operations and apply them to targets.

Previous: Diffing · Next: Serialization

Playground

Open Patch explorer → — generate ops from a diff and apply them interactively.

Problem → approach

Typical painPatch pipeline
Sending full object snapshots over the wire on every editpatch(diffResult) → compact add / remove / replace ops
Mutating shared state when applying updatesapplyPatch() returns a new object; clone the target when you need the original
Undo/redo requires storing full before/after copiesrevertPatch(operations) generates inverse ops for a stack

Generate patch

ts
import { diff, patch } from "@jayoncode/object-diff";

const result = diff(before, after);
const operations = patch(result);
// [{ op: "replace", path: "/name", value: "Jane" }, ...]

Apply patch

ts
import { applyPatch } from "@jayoncode/object-diff";

const updated = applyPatch(structuredClone(before), operations);

Always clone the target if you need to preserve the original.

Revert changes

ts
import { revertPatch } from "@jayoncode/object-diff";

const undoOps = revertPatch(operations);
const restored = applyPatch(structuredClone(after), undoOps);

Common patterns

ScenarioApproach
Optimistic UIApply patch locally; revert on server error
Collaborative editSend patch ops over the wire, not full objects
Undo stackStore patches; revertPatch for undo

Cheat sheet

ts
patch(diffResult); // → operations
applyPatch(target, operations); // → patched object
revertPatch(operations); // → inverse ops

Next: Serialization — export diffs as JSON or Markdown.

JOC is a JayOnCode monorepo of @jayoncode/* packages. Docs sync from source via TypeDoc and sync scripts.