Skip to content

Commit 87843ff

Browse files
committed
chore: update package.json
1 parent dc15ce9 commit 87843ff

File tree

9 files changed

+350
-201
lines changed

9 files changed

+350
-201
lines changed

README.md

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ This library compares two arrays or objects and returns a full diff of their dif
1818

1919
All other existing solutions return a strange diff format that often requires additional parsing. They are also limited to object comparison.
2020

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. 👍
2224

2325
<hr/>
2426

@@ -38,17 +40,21 @@ I am grateful to the generous donors of **Superdiff**!
3840

3941
## FEATURES
4042

41-
**Superdiff** exports 4 functions:
43+
**Superdiff** exports 5 functions:
4244

4345
```ts
44-
// Compares two objects and return a diff for each value and their potential subvalues
46+
// Returns a complete diff of two objects
4547
getObjectDiff(prevObject, nextObject)
46-
// Compares two arrays and returns a diff for each value
48+
49+
// Returns a complete diff of two arrays
4750
getListDiff(prevList, nextList)
51+
4852
// Streams the diff of two object lists, ideal for large lists and maximum performance
4953
streamListDiff(prevList, nextList, referenceProperty)
54+
5055
// Checks whether two values are equal
5156
isEqual(dataA, dataB)
57+
5258
// Checks whether a value is an object
5359
isObject(data)
5460
```
@@ -62,9 +68,9 @@ import { getObjectDiff } from "@donedeal0/superdiff";
6268

6369
Compares two objects and return a diff for each value and their potential subvalues. Supports deeply nested objects with any kind of values.
6470

65-
**Format**
71+
#### FORMAT
6672

67-
input
73+
**Input**
6874

6975
```ts
7076
prevData: Record<string, unknown>;
@@ -78,8 +84,8 @@ options?: {
7884
}
7985
```
8086

81-
- `prevData`: the original object
82-
- `nextData`: the new object
87+
- `prevData`: the original object.
88+
- `nextData`: the new object.
8389
- `options`
8490
- `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.
8591
- `showOnly`: returns only the values whose status you are interested in. It takes two parameters:
@@ -89,7 +95,7 @@ options?: {
8995
- `basic` returns only the main properties whose status matches your query.
9096
- `deep` can return main properties if some of their subproperties' status match your request. The subproperties are filtered accordingly.
9197

92-
output
98+
**Output**
9399

94100
```ts
95101
type ObjectDiff = {
@@ -107,9 +113,9 @@ type Diff = {
107113
diff?: Diff[];
108114
};
109115
```
110-
**Usage**
116+
#### USAGE
111117

112-
input
118+
**Input**
113119

114120
```diff
115121
getObjectDiff(
@@ -134,7 +140,7 @@ getObjectDiff(
134140
);
135141
```
136142

137-
output
143+
**Output**
138144

139145
```diff
140146
{
@@ -202,9 +208,9 @@ import { getListDiff } from "@donedeal0/superdiff";
202208

203209
Compares two arrays and returns a diff for each entry. Supports duplicate values, primitive values and objects.
204210

205-
**Format**
211+
#### FORMAT
206212

207-
input
213+
**Input**
208214

209215
```ts
210216
prevList: T[];
@@ -216,15 +222,15 @@ input
216222
considerMoveAsUpdate?: boolean // false by default
217223
}
218224
```
219-
- `prevList`: the original list
220-
- `nextList`: the new list
225+
- `prevList`: the original list.
226+
- `nextList`: the new list.
221227
- `options`
222228
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
223229
- `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.
224230
- `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`.
226232

227-
output
233+
**Output**
228234

229235
```ts
230236
type ListDiff = {
@@ -239,9 +245,9 @@ type ListDiff = {
239245
}[];
240246
};
241247
```
242-
**Usage**
248+
#### USAGE
243249

244-
input
250+
**Input**
245251

246252
```diff
247253
getListDiff(
@@ -250,7 +256,7 @@ getListDiff(
250256
);
251257
```
252258

253-
output
259+
**Output**
254260

255261
```diff
256262
{
@@ -297,24 +303,24 @@ output
297303
```
298304
<hr/>
299305

300-
### streamListDiff()
306+
### streamListDiff()
301307

302308
```js
303309
import { streamListDiff } from "@donedeal0/superdiff";
304310
```
305311

306312
Streams the diff of two object lists, ideal for large lists and maximum performance.
307313

308-
**Format**
314+
#### FORMAT
309315

310-
input
316+
**Input**
311317

312318
```ts
313-
prevList: T[],
314-
nextList: T[],
315-
referenceProperty: ReferenceProperty<T>,
319+
prevList: Record<string, unknown>[],
320+
nextList: Record<string, unknown>[],
321+
referenceProperty: keyof Record<string, unknown>,
316322
options: {
317-
showOnly?: returns only the values whose status you are interested in. (e.g. `["added", "equal"]`), // [] by default
323+
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
318324
chunksSize?: number, // // 0 by default
319325
considerMoveAsUpdate? boolean; // false by default
320326
}
@@ -324,13 +330,32 @@ input
324330
- `nextList`: the new object list.
325331
- `referenceProperty`: a common property in all the objects of your lists (e.g. `id`).
326332
- `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).
328334
- `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**
330338
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.
332343
333344
```ts
345+
interface StreamListener<T extends Record<string, unknown>> {
346+
on<E extends keyof EmitterEvents<T>>(
347+
event: E,
348+
listener: Listener<EmitterEvents<T>[E]>,
349+
): this;
350+
}
351+
352+
type EmitterEvents<T extends Record<string, unknown>> = {
353+
data: [StreamListDiff<T>[]];
354+
error: [Error];
355+
finish: [];
356+
};
357+
358+
334359
type StreamListDiff<T extends Record<string, unknown>> = {
335360
currentValue: T | null;
336361
previousValue: T | null;
@@ -341,9 +366,9 @@ type StreamListDiff<T extends Record<string, unknown>> = {
341366
};
342367
```
343368
344-
**Usage**
369+
#### USAGE
345370
346-
input
371+
**Input**
347372
348373
```diff
349374
const diff = streamListDiff(
@@ -362,7 +387,7 @@ const diff = streamListDiff(
362387
);
363388
```
364389
365-
output
390+
**Output**
366391
367392
```diff
368393
diff.on("data", (chunk) => {
@@ -407,7 +432,7 @@ diff.on("data", (chunk) => {
407432
});
408433

409434
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))
411436
```
412437
<hr/>
413438
@@ -419,19 +444,22 @@ import { isEqual } from "@donedeal0/superdiff";
419444
420445
Checks whether two values are equal.
421446
422-
**Options**
447+
#### FORMAT
423448
424-
You can add a third `options` parameter to `isEqual`.
449+
**Input**
425450
426451
```ts
427-
{
428-
ignoreArrayOrder?: boolean // false by default,
429-
}
452+
a: unknown,
453+
b: unknown,
454+
options: {
455+
ignoreArrayOrder: boolean; // false by default
456+
},
430457
```
431-
458+
- `a`: the value to compare to the value `b`.
459+
- `b`: the value that will be compared to the value `a`.
432460
- `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.
433461
434-
**Usage**
462+
#### USAGE
435463
436464
437465
```ts
@@ -447,7 +475,7 @@ isEqual(
447475
);
448476
```
449477
450-
output
478+
**Output**
451479
452480
```ts
453481
false;
@@ -462,23 +490,34 @@ import { isObject } from "@donedeal0/superdiff";
462490
463491
Tests whether a value is an object.
464492
465-
**Usage**
493+
#### FORMAT
494+
495+
**Input**
496+
497+
```ts
498+
value: unknown;
499+
```
500+
501+
- `value`: the value whose type will be checked.
466502
467-
input
503+
#### USAGE
504+
505+
**Input**
468506
469507
```ts
470508
isObject(["hello", "world"]);
471509
```
472510
473-
output
511+
**Output**
474512
475513
```ts
476514
false;
477515
```
478516
479517
<hr/>
480518
481-
### More examples are available in the source code tests.
519+
### ℹ️ More examples are available in the source code tests.
520+
482521
483522
<hr/>
484523
@@ -498,4 +537,4 @@ If you or your company uses **Superdiff**, please show your support by becoming
498537
499538
## CONTRIBUTING
500539
501-
Pull requests are welcome!
540+
Issues and pull requests are welcome!

0 commit comments

Comments
 (0)