Object Diff
Typed deep comparison, change records, and JSON Patch generation for structured snapshots.
Install
bash
npm install @jayoncode/object-diffbash
pnpm add @jayoncode/object-diffbash
yarn add @jayoncode/object-diffExample: detect changes and apply a patch
ts
import { diff, hasChanges, patch, applyPatch } from "@jayoncode/object-diff";
const before = { user: { name: "John", role: "viewer" }, active: true };
const after = { user: { name: "Jane", role: "admin" }, active: true };
if (hasChanges(before, after)) {
const result = diff(before, after);
const operations = patch(result);
const synced = applyPatch(before, operations);
// synced matches `after` for tracked paths
}Use diff() for audit trails and inspectors; hasChanges() for dirty checks; patch() / applyPatch() to propagate updates between stores or clients.
Inspect changes interactively →
Problem → approach
| Typical pain | Object Diff |
|---|---|
JSON.stringify(a) !== JSON.stringify(b) — no paths, no types, order-sensitive | diff() returns typed change records with paths (user.name, items[2]) |
| Hand-rolled deep equality and patch logic for optimistic UI | patch() / applyPatch() emit and apply RFC 6902 operations |
| Explaining what changed in PRs or logs requires custom formatting | serialize() exports JSON, Markdown, or table views from the same diff |
Overview
Object Diff compares plain objects and arrays, emits structured change records, and can serialize results or produce RFC 6902-style patch operations.
| API | Purpose |
|---|---|
diff(a, b) | Full change list with paths and types |
hasChanges(a, b) | Boolean dirty check without full diff cost |
patch(diffResult) | Generate patch operations |
applyPatch(target, ops) | Immutable apply |
serialize(diff, format) | JSON, Markdown, or table export |
Documentation path
Foundation
| # | Guide | Topics | Playground |
|---|---|---|---|
| 1 | Tutorial | Install, first diff | Dashboard |
| 2 | Core concepts | Snapshots, changes, patches | Diff |
Core APIs
| # | Guide | Topics | Playground |
|---|---|---|---|
| 3 | Diffing | Options, filtering, helpers | Diff |
| 4 | Patching | Apply, revert, edge cases | Patch |
Output and performance
| # | Guide | Topics | Playground |
|---|---|---|---|
| 5 | Serialization | Export formats | JSON |
| 6 | Performance | Large-object benchmarks | Benchmarks |
Package fit
| Requirement | API |
|---|---|
| Form/state dirty detection | hasChanges(a, b) |
| Structured audit log | diff() change records |
| Partial sync between clients | patch() + applyPatch() |
| Human-readable changelogs | serialize(..., "markdown") |
