@jayoncode/object-diff
v0.3.0
Published
Object Diff — Typed, framework-agnostic deep comparison, change reporting, and patch generation for structured data.
Maintainers
Readme
Object Diff — Typed deep comparison and patch generation for structured data.
Published as @jayoncode/object-diff on npm.
Stop guessing what changed between two objects. Get path-aware change records, fast dirty checks, RFC 6902-style patches, and review-friendly serialization — built for audit trails, optimistic UI, and sync.
Install
npm install @jayoncode/object-diffpnpm add @jayoncode/object-diffThe problem it solves
JSON.stringify(a) !== JSON.stringify(b) tells you something changed — not what, where, or how to sync it.
// ❌ brittle equality / no paths / no patch
JSON.stringify(saved) !== JSON.stringify(draft);@jayoncode/object-diff gives you structured changes you can log, review, and apply.
Quick start — know exactly what changed, then patch it
import { diff, hasChanges, patch, applyPatch, serialize } from "@jayoncode/object-diff";
const saved = {
profile: { name: "Ada", role: "admin" },
prefs: { theme: "dark" },
};
const draft = {
profile: { name: "Ada Lovelace", role: "admin" },
prefs: { theme: "dark" },
};
if (hasChanges(saved, draft)) {
const changes = diff(saved, draft);
await audit.log(serialize(changes, "markdown"));
const synced = applyPatch(saved, patch(changes));
}More problem → solution snippets
Dirty-check before an expensive save
import { hasChanges } from "@jayoncode/object-diff";
if (!hasChanges(serverState, localDraft)) {
return; // nothing to persist
}
await api.save(localDraft);Build an audit-friendly change list
import { diff, serialize } from "@jayoncode/object-diff";
const changes = diff(before, after);
console.log(serialize(changes, "table"));
// path | type | before | afterSync clients with patch operations
import { diff, patch, applyPatch } from "@jayoncode/object-diff";
const operations = patch(diff(clientA, clientB));
const merged = applyPatch(clientA, operations);API
| Function | Description |
| ------------------------------------------ | ------------------------------------------------------- |
| diff(a, b, options?) | Structured change list with metadata |
| compare(a, b, options?) | Deep equality |
| hasChanges(a, b, options?) | Fast boolean dirty check |
| added, removed, updated, unchanged | Filtered change views |
| patch(diff, options?) | Generate patch operations |
| applyPatch(target, patch, options?) | Apply patch immutably |
| revertPatch(target, patch, options?) | Reverse patch operations |
| serialize(diff, format, options?) | Export as json/pretty/markdown/table/html/console/human |
Optional engines (subpaths)
| Import | API |
| ---------------------------------- | ----------------------------------------------------------- |
| @jayoncode/object-diff/core | Slim diff / compare / hasChanges (no patch/serialize) |
| @jayoncode/object-diff/patch | patch / applyPatch / validate / optimize |
| @jayoncode/object-diff/merge | merge |
| @jayoncode/object-diff/query | find / filter / query |
| @jayoncode/object-diff/stats | statistics |
| @jayoncode/object-diff/formatter | serialize / createSerializer |
| @jayoncode/object-diff/plugins | createEngine |
| @jayoncode/object-diff/view | createDiffView (fluent wrapper) |
import { diff } from "@jayoncode/object-diff";
import { createDiffView } from "@jayoncode/object-diff/view";
createDiffView(diff(a, b, { detectMoves: true })).serialize("markdown");Documentation
Repository
https://github.com/itsjayoncode/joc · Package path: packages/object-diff
License
MIT © JayOnCode
