Registry / serialization / diffpatch

diffpatch

JSON →
library0.6.0jsnpmunverified

A JavaScript library for diffing and patching plain objects and arrays, including nested structures. Current stable version is 0.6.0, released as an ES module with TypeScript type definitions. It uses LCS (Longest Common Subsequence) for smart array diffing, supports reverse deltas, unpatching, and optional output formatters. Optionally leverages google-diff-match-patch for long text diffs. Compared to similar libraries, diffpatch focuses on pure JSON delta format and low footprint (~16KB min+gzipped), and requires an objectHash function for object identity matching in arrays.

serializationdata
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Named export only; no default export. Since v0.5.0, ESM is used.

import { DiffPatcher } from 'diffpatch'

Named export for JSON.parse reviver support for Date objects. Works in CommonJS as well.

import { dateReviver } from 'diffpatch'

formatters is an object containing html, annotated, etc. Access via formatters.html, etc. ESM-only exports.

import { formatters } from 'diffpatch'

createPatcher was removed in v0.5.0. Use new DiffPatcher() instead.

import { DiffPatcher } from 'diffpatch'; const patcher = new DiffPatcher()

Basic diff, patch, and unpatch operations with primitive and array values using DiffPatcher.

import { DiffPatcher } from 'diffpatch'; const dp = new DiffPatcher(); const obj1 = { name: 'Alice', age: 30, items: [1, 2, 3] }; const obj2 = { name: 'Bob', age: 30, items: [1, 2, 4] }; const delta = dp.diff(obj1, obj2); console.log('Delta:', JSON.stringify(delta, null, 2)); // { // "name": ["Alice", "Bob"], // "items": { // "_t": "a", // "2": [3, 4] // } // } // Patch obj1 to become obj2 dp.patch(obj1, delta); console.log('Patched obj1:', obj1); // Unpatch to revert dp.unpatch(obj1, delta); console.log('Unpatched obj1:', obj1);
Debug
Known footguns
breakingIn diffpatch v0.5.0, the library switched from CommonJS to ES modules. If you use require('diffpatch'), you must update to dynamic import or use a bundler that supports ESM.
breakingIn diffpatch v0.5.0, the default export was removed. Previously, you could import DiffPatcher as default; now it is a named export only.
deprecatedThe createPatcher function was deprecated in v0.4.0 and removed entirely in v0.5.0. Use the new DiffPatcher() constructor.
gotchaWhen diffing arrays of objects, the default object matching is by position. To match objects by identity, you must provide an objectHash function in the DiffPatcher options. Otherwise, diffs may misinterpret insertions/deletions.
gotchaThe delta format is not backwards compatible with other diff libraries. Do not mix diffpatch deltas with libraries like jsondiffpatch.
gotchaWhen using in Node.js < 8, the library will not work because Node 8+ is required (engines.node >= 8).
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
13 hits · last 30 days
gptbot
3
ahrefsbot
3
amazonbot
3
claudebot
3
bingbot
1
Resources