As of version
0.5.0
this library exposes ES modules. Use an ES module aware transpiler such as Webpack, Rollup or Browserify + babelify to bundle it for the browser.
diffpatch
Diff and patch JavaScript objects.
Usage
; const diffPatcher = ; const man = name: 'Walt' childen: 'Susie' 'Larry' age: 53; const woman = name: 'Clarice' childen: 'Susie' 'Larry' 'Fen' age: 48; const diff = diffPatcher;
Features
- min+gzipped
~16KB
- runs on NodeJS and in the browser
- (optionally) uses google-diff-match-patch for long text diffs (diff at character level)
- smart array diffing using LCS, IMPORTANT NOTE: to match objects inside an array you must provide an
objectHash
function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check Array diff documentation - reverse a delta
- unpatch (eg. revert object to its original state using a delta)
- simplistic, pure JSON, low footprint delta format
- (optional) multiple output formatters
- BONUS:
DiffPatcher#clone(obj)
(deep clone)
Usage
;var diffPatch = ;// sample datavar country =name: "Argentina"capital: "Buenos Aires"independence: 1816 6 9unasur: true;// clone country, using dateReviver for Date objectsvar country2 = JSON;// make some changescountry2name = "Republica Argentina";country2population = 41324992;delete country2capital;var delta = diffPatch;;// patch originaldiffPatch;// reverse diffvar reverseDelta = diffPatch;// also country2 can be return to original value with// diffPatch.unpatch(country2, delta);var delta2 = diffPatch;// undefined => no difference
Array diffing:
; // sample datavar country = name: "Argentina" cities: name: 'Buenos Aires' population: 13028000 name: 'Cordoba' population: 1430023 name: 'Rosario' population: 1136286 name: 'Mendoza' population: 901126 name: 'San Miguel de Tucuman' population: 800000 ; // clone countryvar country2 = JSON; // delete Cordobacountrycities; // add La Platacountrycities; // modify Rosario, and move itvar rosario = countrycities0;rosariopopulation += 1234;countrycities; // create a configured instance, match objects by namevar diffPatcher = { return objname; }; var delta = diffPatcher; ;
For more example cases (nested objects or arrays, long text diffs) check out test/examples/
.
If you want to understand deltas, see delta format documentation.
Installing
npm i diffpatch
Options
You may customize the library by passing a number of options to DiffPatcher
:
const options =// used to match objects when diffing arrays, by default only === operator is used{// this function is used only to when objects are not equal by refreturn obj_id || objid;}arrays:// default true, detect items moved inside the array// (otherwise they will be registered as remove+add)detectMove: true// default false, the value of items moved is not included in deltasincludeValueOnMove: falsetextDiff:// default 60, minimum string length (left and right sides)// to use text diff algorythm: google-diff-match-patchminLength: 60{/*this optional function can be specified to ignore object properties (eg. volatile data)name: property name, present in either context.left or context.right objectscontext: the diff context (has context.left and context.right objects)*/return name !== '$';}cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned(using jsondiffpatch.clone by default), to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching the same objects multiple times without serializing deltas.instead of true, a function can be specified here to provide a custom clone(value)*/;
Diff Formatters
We provide a number of diff formatters, check them out here.
Extend the Library
diff()
, patch()
and reverse()
functions are implemented using Pipes & Filters pattern, making it extremely customizable by adding or replacing filters on a pipe.
Check Plugins documentation for details.
Credits
This library is a fork of jsondiffpatch and borrows heavily from the original implementation.
License
MIT