@@ -15,7 +15,7 @@ Diff & patch JavaScript objects
15
15
## ** [ Live Demo] ( http://benjamine.github.io/jsondiffpatch/demo/index.html ) **
16
16
17
17
- min+gzipped ~ 16KB
18
- - browser and server (` /dist ` folder with bundles for UMD, commonjs, or ES modules )
18
+ - browser and server (ESM-only )
19
19
- (optionally) uses [ google-diff-match-patch] ( http://code.google.com/p/google-diff-match-patch/ ) for long text diffs (diff at character level)
20
20
- 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 )
21
21
- reverse a delta
@@ -31,34 +31,29 @@ Diff & patch JavaScript objects
31
31
32
32
## Supported platforms
33
33
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+
41
36
42
37
## Usage
43
38
44
39
``` javascript
45
40
// sample data
46
- var country = {
41
+ const country = {
47
42
name: ' Argentina' ,
48
43
capital: ' Buenos Aires' ,
49
44
independence: new Date (1816 , 6 , 9 ),
50
45
unasur: true ,
51
46
};
52
47
53
48
// 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 );
55
50
56
51
// make some changes
57
52
country2 .name = ' Republica Argentina' ;
58
53
country2 .population = 41324992 ;
59
54
delete country2 .capital ;
60
55
61
- var delta = jsondiffpatch .diff (country, country2);
56
+ const delta = jsondiffpatch .diff (country, country2);
62
57
63
58
assertSame (delta, {
64
59
name: [' Argentina' , ' Republica Argentina' ], // old value, new value
@@ -70,10 +65,10 @@ assertSame(delta, {
70
65
jsondiffpatch .patch (country, delta);
71
66
72
67
// reverse diff
73
- var reverseDelta = jsondiffpatch .reverse (delta);
68
+ const reverseDelta = jsondiffpatch .reverse (delta);
74
69
// also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta);
75
70
76
- var delta2 = jsondiffpatch .diff (country, country2);
71
+ const delta2 = jsondiffpatch .diff (country, country2);
77
72
assert (delta2 === undefined );
78
73
// undefined => no difference
79
74
```
@@ -82,7 +77,7 @@ Array diffing:
82
77
83
78
``` javascript
84
79
// sample data
85
- var country = {
80
+ const country = {
86
81
name: ' Argentina' ,
87
82
cities: [
88
83
{
@@ -109,7 +104,7 @@ var country = {
109
104
};
110
105
111
106
// clone country
112
- var country2 = JSON .parse (JSON .stringify (country));
107
+ const country2 = JSON .parse (JSON .stringify (country));
113
108
114
109
// delete Cordoba
115
110
country .cities .splice (1 , 1 );
@@ -120,18 +115,18 @@ country.cities.splice(4, 0, {
120
115
});
121
116
122
117
// modify Rosario, and move it
123
- var rosario = country .cities .splice (1 , 1 )[0 ];
118
+ const rosario = country .cities .splice (1 , 1 )[0 ];
124
119
rosario .population += 1234 ;
125
120
country .cities .push (rosario);
126
121
127
122
// create a configured instance, match objects by name
128
- var diffpatcher = jsondiffpatch .create ({
123
+ const diffpatcher = jsondiffpatch .create ({
129
124
objectHash : function (obj ) {
130
125
return obj .name ;
131
126
},
132
127
});
133
128
134
- var delta = diffpatcher .diff (country, country2);
129
+ const delta = diffpatcher .diff (country, country2);
135
130
136
131
assertSame (delta, {
137
132
cities: {
@@ -165,7 +160,7 @@ assertSame(delta, {
165
160
});
166
161
```
167
162
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/`
169
164
170
165
If you want to understand deltas, see [ delta format documentation] ( docs/deltas.md )
171
166
@@ -180,20 +175,19 @@ npm install jsondiffpatch
180
175
```
181
176
182
177
``` js
183
- var jsondiffpatch = require ( ' jsondiffpatch' ) ;
184
- var jsondiffpatchInstance = jsondiffpatch .create (options);
178
+ import * as jsondiffpatch from ' jsondiffpatch' ;
179
+ const jsondiffpatchInstance = jsondiffpatch .create (options);
185
180
```
186
181
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
-
189
182
### browser
190
183
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 ) .
192
185
193
186
## Options
194
187
195
188
``` javascript
196
- var jsondiffpatchInstance = require (' jsondiffpatch' ).create ({
189
+ import * as jsondiffpatch from ' jsondiffpatch' ;
190
+ const jsondiffpatchInstance = jsondiffpatch .create ({
197
191
// used to match objects when diffing arrays, by default only === operator is used
198
192
objectHash : function (obj ) {
199
193
// this function is used only to when objects are not equal by ref
@@ -230,38 +224,40 @@ var jsondiffpatchInstance = require('jsondiffpatch').create({
230
224
<!doctype html>
231
225
<html >
232
226
<head >
233
- <script
234
- type =" text/javascript"
235
- src =" https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"
236
- ></script >
237
227
<link rel =" stylesheet" href =" ./style.css" type =" text/css" />
238
228
<link
239
229
rel =" stylesheet"
240
- href =" ../ formatters- styles/html.css"
230
+ href =" https://cdn.skypack.dev/jsondiffpatch/ formatters/ styles/html.css"
241
231
type =" text/css"
242
232
/>
243
233
<link
244
234
rel =" stylesheet"
245
- href =" ../ formatters- styles/annotated.css"
235
+ href =" https://cdn.skypack.dev/jsondiffpatch/ formatters/ styles/annotated.css"
246
236
type =" text/css"
247
237
/>
248
238
</head >
249
239
<body >
250
240
<div id =" visual" ></div >
251
241
<hr />
252
242
<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);
257
251
258
252
// 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
+ );
261
257
262
258
// self-explained json
263
259
document .getElementById (' annotated' ).innerHTML =
264
- jsondiffpatch . formatters . annotated .format (delta, left);
260
+ annotatedFormatter .format (delta, left);
265
261
</script >
266
262
</body >
267
263
</html >
@@ -275,12 +271,12 @@ For more details check [Formatters documentation](docs/formatters.md)
275
271
276
272
``` sh
277
273
# 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
279
275
280
276
# or install globally
281
277
npm install -g jsondiffpatch
282
278
283
- jsondiffpatch ./demo/left.json ./demo/right.json
279
+ jsondiffpatch ./docs/ demo/left.json ./docs /demo/right.json
284
280
```
285
281
286
282
![ console_demo!] ( docs/demo/consoledemo.png )
0 commit comments