You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+87-48Lines changed: 87 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,9 @@ This library compares two arrays or objects and returns a full diff of their dif
18
18
19
19
All other existing solutions return a strange diff format that often requires additional parsing. They are also limited to object comparison.
20
20
21
-
**Superdiff** gives you a complete diff for both array <u>and</u> objects in a very readable format. Last but not least, it's battle-tested, has zero dependencies, and is super fast. Import. Enjoy. 👍
21
+
**Superdiff** gives you a complete diff for both array <u>and</u> objects in a very readable format. Last but not least, it's battle-tested, has zero dependencies, and is super fast.
22
+
23
+
Import. Enjoy. 👍
22
24
23
25
<hr/>
24
26
@@ -38,17 +40,21 @@ I am grateful to the generous donors of **Superdiff**!
38
40
39
41
## FEATURES
40
42
41
-
**Superdiff** exports 4 functions:
43
+
**Superdiff** exports 5 functions:
42
44
43
45
```ts
44
-
//Compares two objects and return a diff for each value and their potential subvalues
46
+
//Returns a complete diff of two objects
45
47
getObjectDiff(prevObject, nextObject)
46
-
// Compares two arrays and returns a diff for each value
48
+
49
+
// Returns a complete diff of two arrays
47
50
getListDiff(prevList, nextList)
51
+
48
52
// Streams the diff of two object lists, ideal for large lists and maximum performance
@@ -62,9 +68,9 @@ import { getObjectDiff } from "@donedeal0/superdiff";
62
68
63
69
Compares two objects and return a diff for each value and their potential subvalues. Supports deeply nested objects with any kind of values.
64
70
65
-
**Format**
71
+
#### FORMAT
66
72
67
-
input
73
+
**Input**
68
74
69
75
```ts
70
76
prevData: Record<string, unknown>;
@@ -78,8 +84,8 @@ options?: {
78
84
}
79
85
```
80
86
81
-
-`prevData`: the original object
82
-
-`nextData`: the new object
87
+
-`prevData`: the original object.
88
+
-`nextData`: the new object.
83
89
-`options`
84
90
-`ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays have the same value, just not in the same order.
85
91
-`showOnly`: returns only the values whose status you are interested in. It takes two parameters:
@@ -89,7 +95,7 @@ options?: {
89
95
-`basic` returns only the main properties whose status matches your query.
90
96
-`deep` can return main properties if some of their subproperties' status match your request. The subproperties are filtered accordingly.
91
97
92
-
output
98
+
**Output**
93
99
94
100
```ts
95
101
typeObjectDiff= {
@@ -107,9 +113,9 @@ type Diff = {
107
113
diff?:Diff[];
108
114
};
109
115
```
110
-
**Usage**
116
+
#### USAGE
111
117
112
-
input
118
+
**Input**
113
119
114
120
```diff
115
121
getObjectDiff(
@@ -134,7 +140,7 @@ getObjectDiff(
134
140
);
135
141
```
136
142
137
-
output
143
+
**Output**
138
144
139
145
```diff
140
146
{
@@ -202,9 +208,9 @@ import { getListDiff } from "@donedeal0/superdiff";
202
208
203
209
Compares two arrays and returns a diff for each entry. Supports duplicate values, primitive values and objects.
204
210
205
-
**Format**
211
+
#### FORMAT
206
212
207
-
input
213
+
**Input**
208
214
209
215
```ts
210
216
prevList: T[];
@@ -216,15 +222,15 @@ input
216
222
considerMoveAsUpdate?: boolean// false by default
217
223
}
218
224
```
219
-
-`prevList`: the original list
220
-
-`nextList`: the new list
225
+
-`prevList`: the original list.
226
+
-`nextList`: the new list.
221
227
-`options`
222
228
-`showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
223
229
-`referenceProperty` will consider an object to be updated instead of added or deleted if one of its properties remains stable, such as its `id`. This option has no effect on other datatypes.
224
230
-`ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays have the same value, just not in the same order.
225
-
-`considerMoveAsUpdate`: if set to `true`the`moved` value will be considered as `updated`.
231
+
-`considerMoveAsUpdate`: if set to `true`a`moved` value will be considered as `updated`.
Streams the diff of two object lists, ideal for large lists and maximum performance.
307
313
308
-
**Format**
314
+
#### FORMAT
309
315
310
-
input
316
+
**Input**
311
317
312
318
```ts
313
-
prevList: T[],
314
-
nextList: T[],
315
-
referenceProperty: ReferenceProperty<T>,
319
+
prevList: Record<string, unknown>[],
320
+
nextList: Record<string, unknown>[],
321
+
referenceProperty: keyofRecord<string, unknown>,
316
322
options: {
317
-
showOnly?:returnsonlythevalueswhosestatusyouareinterestedin. (e.g. `["added", "equal"]`), // [] by default
323
+
showOnly?:("added"|"deleted"|"moved"|"updated"|"equal")[], // [] by default
318
324
chunksSize?:number, // // 0 by default
319
325
considerMoveAsUpdate?boolean; // false by default
320
326
}
@@ -324,13 +330,32 @@ input
324
330
- `nextList`: the new object list.
325
331
- `referenceProperty`: a common property in all the objects of your lists (e.g. `id`).
326
332
- `options`
327
-
- `chunksSize` the number of object diffs returned by each stream chunk. If set to `0`, each stream will return a single object diff. If set to `10`each stream will return 10 object diffs.
333
+
- `chunksSize` the number of object diffs returned by each streamed chunk. (e.g. `0` = 1 object diff by chunk, `10`= 10 object diffs by chunk).
328
334
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
329
-
- `considerMoveAsUpdate`: if set to `true` the `moved` value will be considered as `updated`.
335
+
- `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`.
336
+
337
+
**Output**
330
338
331
-
output
339
+
The objects diff are grouped in arrays - called `chunks` - and are consumed thanks to an event listener. You have access to 3 events:
340
+
- `data`: to be notified when a new chunk of object diffs is available.
341
+
- `finish`: to be notified when the stream is complete.
342
+
- `error`: to be notified of an error during the stream.
diff.on("finish", () =>console.log("The full diff is available"))
410
-
diff.on("error", (err)=>console.log(err))
435
+
diff.on("error", (err)=>console.log(err))
411
436
```
412
437
<hr/>
413
438
@@ -419,19 +444,22 @@ import { isEqual } from "@donedeal0/superdiff";
419
444
420
445
Checks whether two values are equal.
421
446
422
-
**Options**
447
+
#### FORMAT
423
448
424
-
You can add a third `options` parameter to `isEqual`.
449
+
**Input**
425
450
426
451
```ts
427
-
{
428
-
ignoreArrayOrder?: boolean// false by default,
429
-
}
452
+
a:unknown,
453
+
b: unknown,
454
+
options: {
455
+
ignoreArrayOrder: boolean; // false by default
456
+
},
430
457
```
431
-
458
+
- `a`: the value to compare to the value `b`.
459
+
- `b`: the value that will be compared to the value `a`.
432
460
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays have the same value, just not in the same order.
433
461
434
-
**Usage**
462
+
#### USAGE
435
463
436
464
437
465
```ts
@@ -447,7 +475,7 @@ isEqual(
447
475
);
448
476
```
449
477
450
-
output
478
+
**Output**
451
479
452
480
```ts
453
481
false;
@@ -462,23 +490,34 @@ import { isObject } from "@donedeal0/superdiff";
462
490
463
491
Tests whether a value is an object.
464
492
465
-
**Usage**
493
+
#### FORMAT
494
+
495
+
**Input**
496
+
497
+
```ts
498
+
value: unknown;
499
+
```
500
+
501
+
- `value`: the value whose type will be checked.
466
502
467
-
input
503
+
#### USAGE
504
+
505
+
**Input**
468
506
469
507
```ts
470
508
isObject(["hello", "world"]);
471
509
```
472
510
473
-
output
511
+
**Output**
474
512
475
513
```ts
476
514
false;
477
515
```
478
516
479
517
<hr/>
480
518
481
-
### More examples are available in the source code tests.
519
+
### ℹ️ More examples are available in the source code tests.
520
+
482
521
483
522
<hr/>
484
523
@@ -498,4 +537,4 @@ If you or your company uses **Superdiff**, please show your support by becoming
0 commit comments