Skip to content

Commit 326df11

Browse files
authored
BREAKING CHANGE: cleanup api (#38)
1 parent 8d21774 commit 326df11

21 files changed

Lines changed: 1140 additions & 1529 deletions

README.md

Lines changed: 61 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<img width="722" alt="superdiff-logo" src="https://user-images.githubusercontent.com/43271780/209532864-24d7449e-1185-4810-9423-be5df1fe877f.png">
1+
<img width="1166" height="388" alt="superdiff logo" src="https://github.com/user-attachments/assets/518ddef4-a3c0-43ce-a229-b3008cb2058f" />
22

33

44
[![CI](https://github.com/DoneDeal0/superdiff/actions/workflows/ci.yml/badge.svg)](https://github.com/DoneDeal0/superdiff/actions/workflows/ci.yml)
@@ -8,7 +8,6 @@
88
[![Sponsor](https://img.shields.io/badge/Show%20your%20support-0d1117?style=flat&logo=github-sponsors&logoColor=ea4aaa&color=3F4851)](https://github.com/sponsors/DoneDeal0)
99

1010

11-
1211
<hr/>
1312

1413
# WHAT IS IT?
@@ -21,13 +20,11 @@
2120

2221
## FEATURES
2322

24-
**Superdiff** exports 5 functions:
23+
**Superdiff** exports 3 functions:
2524

2625
- [getObjectDiff](#getobjectdiff)
2726
- [getListDiff](#getlistdiff)
2827
- [streamListDiff](#streamlistdiff)
29-
- [isEqual](#isequal)
30-
- [isObject](#isobject)
3128

3229
<hr/>
3330

@@ -39,6 +36,7 @@
3936
| List diff ||||
4037
| Streaming for huge datasets ||||
4138
| Move detection ||||
39+
| Output refinement ||||
4240
| Zero dependencies ||||
4341

4442
## 📊 BENCHMARK
@@ -127,9 +125,9 @@ type ObjectDiff = {
127125
};
128126

129127
type Diff = {
130-
property: string;
128+
key: string;
129+
value: unknown;
131130
previousValue: unknown;
132-
currentValue: unknown;
133131
status: "added" | "deleted" | "equal" | "updated";
134132
// recursive diff in case of subproperties
135133
diff?: Diff[];
@@ -170,49 +168,49 @@ getObjectDiff(
170168
+ status: "updated",
171169
diff: [
172170
{
173-
property: "id",
171+
key: "id",
172+
value: 54,
174173
previousValue: 54,
175-
currentValue: 54,
176174
status: "equal",
177175
},
178176
{
179-
property: "user",
180-
previousValue: {
177+
key: "user",
178+
value: {
181179
name: "joe",
182-
member: true,
183-
hobbies: ["golf", "football"],
180+
member: false,
181+
hobbies: ["golf", "chess"],
184182
age: 66,
185183
},
186-
currentValue: {
184+
previousValue: {
187185
name: "joe",
188-
member: false,
189-
hobbies: ["golf", "chess"],
186+
member: true,
187+
hobbies: ["golf", "football"],
190188
age: 66,
191189
},
192190
+ status: "updated",
193191
diff: [
194192
{
195-
property: "name",
193+
key: "name",
194+
value: "joe",
196195
previousValue: "joe",
197-
currentValue: "joe",
198196
status: "equal",
199197
},
200198
+ {
201-
+ property: "member",
199+
+ key: "member",
200+
+ value: false,
202201
+ previousValue: true,
203-
+ currentValue: false,
204202
+ status: "updated",
205203
+ },
206204
+ {
207-
+ property: "hobbies",
205+
+ key: "hobbies",
206+
+ value: ["golf", "chess"],
208207
+ previousValue: ["golf", "football"],
209-
+ currentValue: ["golf", "chess"],
210208
+ status: "updated",
211209
+ },
212210
{
213-
property: "age",
211+
key: "age",
212+
value: 66,
214213
previousValue: 66,
215-
currentValue: 66,
216214
status: "equal",
217215
},
218216
],
@@ -239,7 +237,7 @@ Compares two arrays and returns a diff for each entry. Supports duplicate values
239237
nextList: T[];
240238
options?: {
241239
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
242-
referenceProperty?: string, // "" by default
240+
referenceKey?: string, // "" by default
243241
ignoreArrayOrder?: boolean, // false by default,
244242
considerMoveAsUpdate?: boolean // false by default
245243
}
@@ -248,7 +246,7 @@ Compares two arrays and returns a diff for each entry. Supports duplicate values
248246
- `nextList`: the new list.
249247
- `options`
250248
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
251-
- `referenceProperty` will consider an object to be `updated` rather than `added` or `deleted` if one of its properties remains stable, such as its `id`. This option has no effect on other datatypes.
249+
- `referenceKey` will consider an object to be `updated` rather than `added` or `deleted` if one of its properties remains stable, such as its `id`. This option has no effect on other datatypes.
252250
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays contain the same values, just in a different order.
253251
- `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`.
254252

@@ -260,9 +258,8 @@ type ListDiff = {
260258
status: "added" | "deleted" | "equal" | "moved" | "updated";
261259
diff: {
262260
value: unknown;
263-
prevIndex: number | null;
264-
newIndex: number | null;
265-
indexDiff: number | null;
261+
index: number | null;
262+
previousIndex: number | null;
266263
status: "added" | "deleted" | "equal" | "moved" | "updated";
267264
}[];
268265
};
@@ -287,37 +284,32 @@ getListDiff(
287284
diff: [
288285
{
289286
value: "mbappe",
290-
prevIndex: 0,
291-
newIndex: 0,
292-
indexDiff: 0,
287+
index: 0,
288+
previousIndex: 0,
293289
status: "equal",
294290
},
295291
- {
296292
- value: "mendes",
297-
- prevIndex: 1,
298-
- newIndex: null,
299-
- indexDiff: null,
293+
- index: null,
294+
- previousIndex: 1,
300295
- status: "deleted",
301296
- },
302297
- {
303298
- value: "verratti",
304-
- prevIndex: 2,
305-
- newIndex: null,
306-
- indexDiff: null,
299+
- index: null,
300+
- previousIndex: 2,
307301
- status: "deleted",
308302
- },
309303
+ {
310304
+ value: "messi",
311-
+ prevIndex: null,
312-
+ newIndex: 1,
313-
+ indexDiff: null,
305+
+ index: 1,
306+
+ previousIndex: null,
314307
+ status: "added",
315308
+ },
316309
+ {
317310
+ value: "ruiz",
318-
+ prevIndex: 3,
319-
+ newIndex: 2,
320-
+ indexDiff: -1,
311+
+ index: 2,
312+
+ previousIndex: 3,
321313
+ status: "moved",
322314
},
323315
],
@@ -349,7 +341,7 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
349341
```ts
350342
prevList: Readable | FilePath | Record<string, unknown>[],
351343
nextList: Readable | FilePath | Record<string, unknown>[],
352-
referenceProperty: keyof Record<string, unknown>,
344+
referenceKey: keyof Record<string, unknown>,
353345
options: {
354346
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
355347
chunksSize?: number, // 0 by default
@@ -366,7 +358,7 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
366358
```ts
367359
prevList: ReadableStream<Record<string, unknown>> | File | Record<string, unknown>[],
368360
nextList: ReadableStream<Record<string, unknown>> | File | Record<string, unknown>[],
369-
referenceProperty: keyof Record<string, unknown>,
361+
referenceKey: keyof Record<string, unknown>,
370362
options: {
371363
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
372364
chunksSize?: number, // 0 by default
@@ -379,7 +371,7 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
379371

380372
- `prevList`: the original object list.
381373
- `nextList`: the new object list.
382-
- `referenceProperty`: a property common to all objects in your lists (e.g. `id`).
374+
- `referenceKey`: a key common to all objects in your lists (e.g. `id`).
383375
- `options`
384376
- `chunksSize` the number of object diffs returned by each streamed chunk. (e.g. `0` = 1 object diff per chunk, `10` = 10 object diffs per chunk).
385377
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
@@ -404,11 +396,10 @@ interface StreamListener<T> {
404396
}
405397

406398
type StreamListDiff<T extends Record<string, unknown>> = {
407-
currentValue: T | null;
399+
value: T | null;
400+
index: number | null;
408401
previousValue: T | null;
409-
prevIndex: number | null;
410-
newIndex: number | null;
411-
indexDiff: number | null;
402+
previousIndex: number | null;
412403
status: "added" | "deleted" | "moved" | "updated" | "equal";
413404
};
414405
```
@@ -475,39 +466,35 @@ diff.on("data", (chunk) => {
475466
// first chunk received (2 object diffs)
476467
[
477468
+ {
469+
+ value: { id: 0, name: "Item 0" },
470+
+ index: 0,
478471
+ previousValue: null,
479-
+ currentValue: { id: 0, name: 'Item 0' },
480-
+ prevIndex: null,
481-
+ newIndex: 0,
482-
+ indexDiff: null,
483-
+ status: 'added'
472+
+ previousIndex: null,
473+
+ status: "added"
484474
+ },
485475
- {
486-
- previousValue: { id: 1, name: 'Item 1' },
487-
- currentValue: null,
488-
- prevIndex: 0,
489-
- newIndex: null,
490-
- indexDiff: null,
491-
- status: 'deleted'
476+
- value: null,
477+
- index: null,
478+
- previousValue: { id: 1, name: "Item 1" },
479+
- previousIndex: 0,
480+
- status: "deleted"
492481
- }
493482
]
494483
// second chunk received (2 object diffs)
495484
[
496485
{
497-
previousValue: { id: 2, name: 'Item 2' },
498-
currentValue: { id: 2, name: 'Item 2' },
499-
prevIndex: 1,
500-
newIndex: 1,
501-
indexDiff: 0,
502-
status: 'equal'
486+
value: { id: 2, name: "Item 2" },
487+
index: 1,
488+
previousValue: { id: 2, name: "Item 2" },
489+
previousIndex: 1,
490+
status: "equal"
503491
},
504492
+ {
505-
+ previousValue: { id: 3, name: 'Item 3' },
506-
+ currentValue: { id: 3, name: 'Item Three' },
507-
+ prevIndex: 2,
508-
+ newIndex: 2,
509-
+ indexDiff: 0,
510-
+ status: 'updated'
493+
+ value: { id: 3, name: "Item Three" },
494+
+ index: 2,
495+
+ previousValue: { id: 3, name: "Item 3" },
496+
+ previousIndex: 2,
497+
+ status: "updated"
511498
+ },
512499
]
513500
});
@@ -518,89 +505,8 @@ diff.on("error", (err) => console.log(err))
518505

519506
<hr/>
520507

521-
### isEqual
522-
523-
```js
524-
import { isEqual } from "@donedeal0/superdiff";
525-
```
526-
527-
Tests whether two values are equal.
528-
529-
#### FORMAT
530-
531-
**Input**
532-
533-
```ts
534-
a: unknown,
535-
b: unknown,
536-
options: {
537-
ignoreArrayOrder: boolean; // false by default
538-
},
539-
```
540-
- `a`: the value to be compared to the value `b`.
541-
- `b`: the value to be compared to the value `a`.
542-
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays contain the same values, just in a different order.
543-
544-
#### USAGE
545-
546-
547-
```ts
548-
isEqual(
549-
[
550-
{ name: "joe", age: 99 },
551-
{ name: "nina", age: 23 },
552-
],
553-
[
554-
{ name: "joe", age: 98 },
555-
{ name: "nina", age: 23 },
556-
],
557-
);
558-
```
559-
560-
**Output**
561-
562-
```ts
563-
false;
564-
```
565-
<hr/>
566-
567-
### isObject
568-
569-
```js
570-
import { isObject } from "@donedeal0/superdiff";
571-
```
572-
573-
Tests whether a value is an object.
574-
575-
#### FORMAT
576-
577-
**Input**
578-
579-
```ts
580-
value: unknown;
581-
```
582-
583-
- `value`: the value whose type will be checked.
584-
585-
#### USAGE
586-
587-
**Input**
588-
589-
```ts
590-
isObject(["hello", "world"]);
591-
```
592-
593-
**Output**
594-
595-
```ts
596-
false;
597-
```
598-
599-
<hr/>
600-
601508
### ℹ️ More examples are available in the source code tests.
602509

603-
604510
<hr/>
605511

606512
## CREDITS

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@donedeal0/superdiff",
3-
"version": "3.2.0",
3+
"version": "4.0.0",
44
"type": "module",
55
"description": "Superdiff provides a rich and readable diff for both arrays and objects. It supports stream and file inputs for handling large datasets efficiently, is battle-tested, has zero dependencies, and offer a top-tier performance.",
66
"main": "dist/index.js",

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export { getObjectDiff } from "./lib/object-diff";
22
export { getListDiff } from "./lib/list-diff";
3-
export { isEqual, isObject } from "./lib/utils";
43
export * from "./models/list";
54
export * from "./models/object";
65
export * from "./models/stream";

0 commit comments

Comments
 (0)