Skip to content

Commit 44ad4c2

Browse files
committed
docs: update mentions of formatters and code blocks
1 parent f1de90d commit 44ad4c2

File tree

6 files changed

+57
-44
lines changed

6 files changed

+57
-44
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
- min+gzipped ~ 16KB
2525
- browser and server (ESM-only)
2626
- deep diff, use delta to patch
27-
- (optionally) uses [google-diff-match-patch](http://code.google.com/p/google-diff-match-patch/) for long text diffs (diff at character level)
2827
- 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)
28+
- (optionally) text diffing of long strings powered by [google-diff-match-patch](http://code.google.com/p/google-diff-match-patch/) (diff at character level)
2929
- reverse a delta, unpatch (eg. revert object to its original state using a delta)
30-
- simplistic, pure JSON, low footprint [delta format](docs/deltas.md)
31-
- multiple output formatters:
32-
- html (check it at the [Live Demo](https://jsondiffpatch.com))
33-
- annotated json (html), makes the JSON delta format self-explained
30+
- multiple output formats:
31+
- pure JSON, low footprint [delta format](docs/deltas.md)
32+
- annotated JSON (html), to help explain the format with annotations
33+
- JSON Patch ([RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)), can generate patches, and also apply them
3434
- console (colored), try running `./node_modules/.bin/jsondiffpatch left.json right.json`
35-
- JSON Patch format RFC 6902 support
3635
- write your own! check [Formatters documentation](docs/formatters.md)
3736
- BONUS: `jsondiffpatch.clone(obj)` (deep clone)
3837

@@ -53,8 +52,7 @@ npx jsondiffpatch https://jsondiffpatch.com/demo/left.json https://jsondiffpatch
5352

5453
or as a library:
5554

56-
```javascript
57-
// sample data
55+
```ts// sample data
5856
const country = {
5957
name: 'Argentina',
6058
capital: 'Buenos Aires',
@@ -92,8 +90,7 @@ assert(delta2 === undefined);
9290

9391
Array diffing:
9492

95-
```javascript
96-
// sample data
93+
```ts// sample data
9794
const country = {
9895
name: 'Argentina',
9996
cities: [
@@ -202,8 +199,7 @@ In a browser, you can load a bundle using a tool like [esm.sh](https://esm.sh) o
202199

203200
## Options
204201

205-
```javascript
206-
import * as jsondiffpatch from 'jsondiffpatch';
202+
```tsimport * as jsondiffpatch from 'jsondiffpatch';
207203
208204
// Only import if you want text diffs using diff-match-patch
209205
import DiffMatchPatch from 'diff-match-patch';

demos/html-demo/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,17 @@ <h2>Features</h2>
400400
plain text
401401
</li>
402402
<li>
403-
json diff as visual diff or json (see
403+
output visual diff, json (see
404404
<a
405405
href="https://github.com/benjamine/jsondiffpatch/blob/master/docs/deltas.md"
406406
rel="nofollow"
407407
target="_blank"
408408
>delta format</a
409+
>), or json patch (<a
410+
href="https://datatracker.ietf.org/doc/html/rfc6902"
411+
target="_blank"
412+
rel="nofollow"
413+
>RFC 6902</a
409414
>)
410415
</li>
411416
<li>

docs/arrays.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ To improve the results leveraging the power of LCS (and position move detection)
2222

2323
### An example using objectHash
2424

25-
```javascript
26-
var delta = jsondiffpatch
25+
```ts
26+
const delta = jsondiffpatch
2727
.create({
2828
objectHash: function (obj, index) {
2929
// try to find an id property, otherwise just use the index in the array
@@ -47,8 +47,8 @@ This introduces a few benefits:
4747

4848
moves are detected by default, you can turn move detection off with:
4949

50-
```javascript
51-
var customDiffPatch = jsondiffpatch.create({
50+
```ts
51+
const customDiffPatch = jsondiffpatch.create({
5252
arrays: {
5353
detectMove: false,
5454
},

docs/deltas.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ Here's a complete reference of this format.
1616

1717
a value was added, i.e. it was `undefined` and now has a value.
1818

19-
```javascript
19+
```ts
2020
delta = [newValue];
2121
```
2222

2323
## Modified
2424

2525
a value was replaced by another value
2626

27-
```javascript
27+
```ts
2828
delta = [oldValue, newValue];
2929
```
3030

3131
## Deleted
3232

3333
a value was deleted, i.e. it had a value and is now `undefined`
3434

35-
```javascript
35+
```ts
3636
delta = [oldValue, 0, 0];
3737
```
3838

@@ -44,7 +44,7 @@ This makes the delta irreversible (can't be used for unpatch), but might be a go
4444

4545
value is an object, and there are nested changes inside its properties
4646

47-
```javascript
47+
```ts
4848
delta = {
4949
property1: innerDelta1,
5050
property2: innerDelta2,
@@ -56,19 +56,19 @@ delta = {
5656
5757
Here's an example combining what we have:
5858

59-
```
59+
```ts
6060
delta = {
61-
property1: [ newValue1 ], // obj[property1] = newValue1
62-
property2: [ oldValue2, newValue2 ], // obj[property2] = newValue2 (and previous value was oldValue2)
63-
property5: [ oldValue5, 0, 0 ] // delete obj[property5] (and previous value was oldValue5)
64-
}
61+
property1: [newValue1], // obj[property1] = newValue1
62+
property2: [oldValue2, newValue2], // obj[property2] = newValue2 (and previous value was oldValue2)
63+
property5: [oldValue5, 0, 0], // delete obj[property5] (and previous value was oldValue5)
64+
};
6565
```
6666

6767
## Array with inner changes
6868

6969
value is an array, and there are nested changes inside its items
7070

71-
```javascript
71+
```ts
7272
delta = {
7373
_t: 'a',
7474
index1: innerDelta1,
@@ -92,7 +92,7 @@ Indices on array deltas can be expressed in two ways:
9292

9393
an item was moved to a different position in the same array
9494

95-
```javascript
95+
```ts
9696
delta = ['', destinationIndex, 3];
9797
```
9898

@@ -104,16 +104,16 @@ delta = ['', destinationIndex, 3];
104104

105105
If two strings are compared and they are different, you will see as you expect:
106106

107-
```javascript
107+
```ts
108108
delta = ['some text', 'some text modified'];
109109
```
110110

111111
But if both strings are long enough, [a text diffing algorithm](https://code.google.com/p/google-diff-match-patch/) will be used to efficiently detect changes in parts of the text.
112112

113113
You can modify the minimum length with:
114114

115-
```javascript
116-
var customDiffPatch = jsondiffpatch.create({
115+
```ts
116+
const customDiffPatch = jsondiffpatch.create({
117117
textDiff: {
118118
minLength: 60, // default value
119119
},
@@ -122,7 +122,7 @@ var customDiffPatch = jsondiffpatch.create({
122122

123123
And the delta will look like this:
124124

125-
```javascript
125+
```ts
126126
delta = [unidiff, 0, 2];
127127
```
128128

docs/formatters.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Some formatters are included that let you convert a JSON delta into other format
66

77
add `build/formatters.js` and `src/formatters/html.css` to your page, and:
88

9-
```javascript
10-
var delta = jsondiffpatch.diff(left, right);
9+
```ts
10+
const delta = jsondiffpatch.diff(left, right);
1111
// left is optional, if specified unchanged values will be visible too
1212
document.getElementBy('the-diff').innerHTML =
1313
jsondiffpatch.formatters.html.format(delta, left);
@@ -28,8 +28,8 @@ This will render the original JSON delta in html, with annotations aside explain
2828

2929
add `build/formatters.js` and `src/formatters/annotated.css` to your page, and:
3030

31-
```javascript
32-
var delta = jsondiffpatch.diff(left, right);
31+
```ts
32+
const delta = jsondiffpatch.diff(left, right);
3333
document.getElementBy('the-diff').innerHTML =
3434
jsondiffpatch.formatters.annotated.format(delta);
3535
```
@@ -44,9 +44,9 @@ colored text to console log, it's used by the CLI:
4444

4545
but you can use it programmatically too:
4646

47-
```javascript
48-
var delta = jsondiffpatch.diff(left, right);
49-
var output = jsondiffpatch.formatters.console.format(delta);
47+
```ts
48+
const delta = jsondiffpatch.diff(left, right);
49+
const output = jsondiffpatch.formatters.console.format(delta);
5050
console.log(output);
5151

5252
// or simply
@@ -55,14 +55,26 @@ jsondiffpatch.console.log(delta);
5555

5656
## JSON PATCH (RFC 6902)
5757

58-
```javascript
59-
var delta = jsondiffpatch.diff(left, right);
60-
var output = jsondiffpatch.formatters.jsonpatch.format(delta);
61-
console.log(output);
58+
```ts
59+
const delta = jsondiffpatch.diff(left, right);
60+
const patch = jsondiffpatch.formatters.jsonpatch.format(delta);
61+
console.log(patch);
6262
```
6363

6464
_Don't use with `textDiff` as it isn't suppported_
6565

66+
an implementation of patch method is also provided:
67+
68+
```ts
69+
const target = jsondiffpatch.clone(left);
70+
const patched = jsondiffpatch.formatters.jsonpatch.patch(target, patch);
71+
72+
// target is now equals to right
73+
assert(JSON.stringify(patched), JSON.stringify(right));
74+
```
75+
76+
Note: this patch method is atomic as specified by [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902#section-5). If any error occurs during patching, the `target` object is rolled back to its original state.
77+
6678
## Create one
6779

6880
Of course, first step to create a formatters is understanding the [delta format](deltas.md).

docs/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Check the `/src/filters` folder for filter examples.
1717
Here is an example to provide number differences in deltas (when left and right values are both numbers)
1818
This, way when diffing 2 numbers instead of obtaining `[ oldValue, newValue ] `, the difference between both values will be saved, this could be useful for counters simultaneously incremented in multiple client applications (patches that both increment a value would be combined, instead of failing with a conflict).
1919

20-
```javascript
20+
```ts
2121
/*
2222
Plugin a new diff filter
2323
*/

0 commit comments

Comments
 (0)