@@ -15,7 +15,7 @@ Diff & patch JavaScript objects
1515## ** [ Live Demo] ( http://benjamine.github.io/jsondiffpatch/demo/index.html ) **
1616
1717- min+gzipped ~ 16KB
18- - browser and server (` /dist ` folder with bundles for UMD, commonjs, or ES modules )
18+ - browser and server (ESM-only )
1919- (optionally) uses [ google-diff-match-patch] ( http://code.google.com/p/google-diff-match-patch/ ) for long text diffs (diff at character level)
2020- smart array diffing using [ LCS] ( http://en.wikipedia.org/wiki/Longest_common_subsequence_problem ) , ** _ 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] ( docs/arrays.md )
2121- reverse a delta
@@ -31,34 +31,29 @@ Diff & patch JavaScript objects
3131
3232## Supported platforms
3333
34- - Any modern browser and IE8+
35-
36- [ ![ Testling Status] ( https://ci.testling.com/benjamine/jsondiffpatch.png )] ( https://ci.testling.com/benjamine/jsondiffpatch )
37-
38- And you can test your current browser visiting the [ test page] ( http://benjamine.github.io/jsondiffpatch/test/index.html ) .
39-
40- - Node.js [ ![ Build Status] ( https://secure.travis-ci.org/benjamine/jsondiffpatch.svg )] ( http://travis-ci.org/benjamine/jsondiffpatch ) v8+
34+ - Any browser that supports ES6
35+ - Node.js 18, 20+
4136
4237## Usage
4338
4439``` javascript
4540// sample data
46- var country = {
41+ const country = {
4742 name: ' Argentina' ,
4843 capital: ' Buenos Aires' ,
4944 independence: new Date (1816 , 6 , 9 ),
5045 unasur: true ,
5146};
5247
5348// clone country, using dateReviver for Date objects
54- var country2 = JSON .parse (JSON .stringify (country), jsondiffpatch .dateReviver );
49+ const country2 = JSON .parse (JSON .stringify (country), jsondiffpatch .dateReviver );
5550
5651// make some changes
5752country2 .name = ' Republica Argentina' ;
5853country2 .population = 41324992 ;
5954delete country2 .capital ;
6055
61- var delta = jsondiffpatch .diff (country, country2);
56+ const delta = jsondiffpatch .diff (country, country2);
6257
6358assertSame (delta, {
6459 name: [' Argentina' , ' Republica Argentina' ], // old value, new value
@@ -70,10 +65,10 @@ assertSame(delta, {
7065jsondiffpatch .patch (country, delta);
7166
7267// reverse diff
73- var reverseDelta = jsondiffpatch .reverse (delta);
68+ const reverseDelta = jsondiffpatch .reverse (delta);
7469// also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta);
7570
76- var delta2 = jsondiffpatch .diff (country, country2);
71+ const delta2 = jsondiffpatch .diff (country, country2);
7772assert (delta2 === undefined );
7873// undefined => no difference
7974```
@@ -82,7 +77,7 @@ Array diffing:
8277
8378``` javascript
8479// sample data
85- var country = {
80+ const country = {
8681 name: ' Argentina' ,
8782 cities: [
8883 {
@@ -109,7 +104,7 @@ var country = {
109104};
110105
111106// clone country
112- var country2 = JSON .parse (JSON .stringify (country));
107+ const country2 = JSON .parse (JSON .stringify (country));
113108
114109// delete Cordoba
115110country .cities .splice (1 , 1 );
@@ -120,18 +115,18 @@ country.cities.splice(4, 0, {
120115});
121116
122117// modify Rosario, and move it
123- var rosario = country .cities .splice (1 , 1 )[0 ];
118+ const rosario = country .cities .splice (1 , 1 )[0 ];
124119rosario .population += 1234 ;
125120country .cities .push (rosario);
126121
127122// create a configured instance, match objects by name
128- var diffpatcher = jsondiffpatch .create ({
123+ const diffpatcher = jsondiffpatch .create ({
129124 objectHash : function (obj ) {
130125 return obj .name ;
131126 },
132127});
133128
134- var delta = diffpatcher .diff (country, country2);
129+ const delta = diffpatcher .diff (country, country2);
135130
136131assertSame (delta, {
137132 cities: {
@@ -165,7 +160,7 @@ assertSame(delta, {
165160});
166161```
167162
168- For more example cases (nested objects or arrays, long text diffs) check ` test/examples/ `
163+ For more example cases (nested objects or arrays, long text diffs) check ` packages/jsondiffpatch/ test/examples/`
169164
170165If you want to understand deltas, see [ delta format documentation] ( docs/deltas.md )
171166
@@ -180,20 +175,19 @@ npm install jsondiffpatch
180175```
181176
182177``` js
183- var jsondiffpatch = require ( ' jsondiffpatch' ) ;
184- var jsondiffpatchInstance = jsondiffpatch .create (options);
178+ import * as jsondiffpatch from ' jsondiffpatch' ;
179+ const jsondiffpatchInstance = jsondiffpatch .create (options);
185180```
186181
187- Some properties are available only from static main module (e.g. formatters, console), so we need to keep the reference to it if we want to use them.
188-
189182### browser
190183
191- In a browser, you could load directly a bundle in ` /dist ` , eg. ` /dist/jsondiffpatch.umd.js ` .
184+ In a browser, you can load a bundle using a tool like [ esm.sh ] ( https://esm.sh ) or [ Skypack ] ( https://www.skypack.dev ) .
192185
193186## Options
194187
195188``` javascript
196- var jsondiffpatchInstance = require (' jsondiffpatch' ).create ({
189+ import * as jsondiffpatch from ' jsondiffpatch' ;
190+ const jsondiffpatchInstance = jsondiffpatch .create ({
197191 // used to match objects when diffing arrays, by default only === operator is used
198192 objectHash : function (obj ) {
199193 // this function is used only to when objects are not equal by ref
@@ -230,38 +224,40 @@ var jsondiffpatchInstance = require('jsondiffpatch').create({
230224<!doctype html>
231225<html >
232226 <head >
233- <script
234- type =" text/javascript"
235- src =" https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"
236- ></script >
237227 <link rel =" stylesheet" href =" ./style.css" type =" text/css" />
238228 <link
239229 rel =" stylesheet"
240- href =" ../ formatters- styles/html.css"
230+ href =" https://cdn.skypack.dev/jsondiffpatch/ formatters/ styles/html.css"
241231 type =" text/css"
242232 />
243233 <link
244234 rel =" stylesheet"
245- href =" ../ formatters- styles/annotated.css"
235+ href =" https://cdn.skypack.dev/jsondiffpatch/ formatters/ styles/annotated.css"
246236 type =" text/css"
247237 />
248238 </head >
249239 <body >
250240 <div id =" visual" ></div >
251241 <hr />
252242 <div id =" annotated" ></div >
253- <script >
254- var left = { a: 3 , b: 4 };
255- var right = { a: 5 , c: 9 };
256- var delta = jsondiffpatch .diff (left, right);
243+ <script type =" module" >
244+ import * as jsondiffpatch from ' https://cdn.skypack.dev/jsondiffpatch' ;
245+ import * as annotatedFormatter from ' https://cdn.skypack.dev/jsondiffpatch/formatters/annotated' ;
246+ import * as htmlFormatter from ' https://cdn.skypack.dev/jsondiffpatch/formatters/html' ;
247+
248+ const left = { a: 3 , b: 4 };
249+ const right = { a: 5 , c: 9 };
250+ const delta = jsondiffpatch .diff (left, right);
257251
258252 // beautiful html diff
259- document .getElementById (' visual' ).innerHTML =
260- jsondiffpatch .formatters .html .format (delta, left);
253+ document .getElementById (' visual' ).innerHTML = htmlFormatter .format (
254+ delta,
255+ left,
256+ );
261257
262258 // self-explained json
263259 document .getElementById (' annotated' ).innerHTML =
264- jsondiffpatch . formatters . annotated .format (delta, left);
260+ annotatedFormatter .format (delta, left);
265261 </script >
266262 </body >
267263</html >
@@ -275,12 +271,12 @@ For more details check [Formatters documentation](docs/formatters.md)
275271
276272``` sh
277273# diff two json files, colored output (using chalk lib)
278- ./node_modules/.bin/jsondiffpatch ./left.json ./right.json
274+ ./node_modules/.bin/jsondiffpatch ./docs/demo/ left.json ./docs/demo /right.json
279275
280276# or install globally
281277npm install -g jsondiffpatch
282278
283- jsondiffpatch ./demo/left.json ./demo/right.json
279+ jsondiffpatch ./docs/ demo/left.json ./docs /demo/right.json
284280```
285281
286282![ console_demo!] ( docs/demo/consoledemo.png )
0 commit comments