Skip to content

Commit 4d22c68

Browse files
authored
feat: handle stream and file inputs (#28)
* feat: handle stream and file inputs * chore: update jsdoc * feat: v3.0.0 BREAKING CHANGE: streamListDiff is now imported from @donedeal0/superdiff/client or @donedeal/superdiff/server depending on your environment
1 parent 27306f7 commit 4d22c68

17 files changed

+2755
-532
lines changed

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ jest.config.js
55
package-lock.json
66
README.md
77
tsconfig.json
8-
tsup.config.ts

README.md

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

1919
Most existing solutions return a confusing diff format that often requires extra parsing. They are also limited to object comparison.
2020

21-
**Superdiff** provides a complete and readable diff for both arrays **and** objects. Plus, it's battle-tested, has zero dependencies, and is super fast.
21+
**Superdiff** provides a complete and readable diff for both arrays **and** objects. Plus, it supports stream and file inputs for handling large datasets efficiently, is battle-tested, has zero dependencies, and is super fast.
2222

2323
Import. Enjoy. 👍
2424

@@ -306,7 +306,10 @@ getListDiff(
306306
### streamListDiff()
307307

308308
```js
309-
import { streamListDiff } from "@donedeal0/superdiff";
309+
// If you are in a server environment
310+
import { streamListDiff } from "@donedeal0/superdiff/server";
311+
// If you are in a browser environment
312+
import { streamListDiff } from "@donedeal0/superdiff/client";
310313
```
311314

312315
Streams the diff of two object lists, ideal for large lists and maximum performance.
@@ -315,14 +318,33 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
315318

316319
**Input**
317320

321+
#### Server
322+
323+
> In a server environment, `Readable` refers to Node.js streams, and `FilePath` refers to the path of a file (e.g., `./list.json`). Examples are provided in the #usage section below.
324+
325+
```ts
326+
prevList: Readable | FilePath | Record<string, unknown>[],
327+
nextList: Readable | FilePath | Record<string, unknown>[],
328+
referenceProperty: keyof Record<string, unknown>,
329+
options: {
330+
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
331+
chunksSize?: number, // 0 by default
332+
considerMoveAsUpdate?: boolean; // false by default
333+
}
334+
```
335+
336+
#### Browser
337+
338+
> In a browser environment, `ReadableStream` refers to the browser's streaming API, and `File` refers to an uploaded or local file. Examples are provided in the #usage section below.
339+
318340
```ts
319-
prevList: Record<string, unknown>[],
320-
nextList: Record<string, unknown>[],
341+
prevList: ReadableStream<Record<string, unknown>> | File | Record<string, unknown>[],
342+
nextList: ReadableStream<Record<string, unknown>> | File | Record<string, unknown>[],
321343
referenceProperty: keyof Record<string, unknown>,
322344
options: {
323345
showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default
324-
chunksSize?: number, // // 0 by default
325-
considerMoveAsUpdate? boolean; // false by default
346+
chunksSize?: number, // 0 by default
347+
considerMoveAsUpdate?: boolean; // false by default
326348
}
327349
```
328350

@@ -370,6 +392,40 @@ type StreamListDiff<T extends Record<string, unknown>> = {
370392

371393
**Input**
372394

395+
You can send streams, file paths, or arrays as input:
396+
397+
> If you are in a server environment
398+
399+
```ts
400+
// for a simple array
401+
const stream = [{ id: 1, name: "hello" }]
402+
// for a large array
403+
const stream = Readable.from(list, { objectMode: true });
404+
// for a local file
405+
const stream = path.resolve(__dirname, "./list.json");
406+
407+
```
408+
409+
> If you are in a browser environment
410+
411+
```ts
412+
// for a simple array
413+
const stream = [{ id: 1, name: "hello" }]
414+
// for a large array
415+
const stream = new ReadableStream({
416+
start(controller) {
417+
list.forEach((value) => controller.enqueue(value));
418+
controller.close();
419+
},
420+
});
421+
// for a local file
422+
const stream = new File([JSON.stringify(file)], "file.json", { type: "application/json" });
423+
// for a file input
424+
const stream = e.target.files[0];
425+
426+
```
427+
> Example
428+
373429
```diff
374430
const diff = streamListDiff(
375431
[
@@ -431,9 +487,10 @@ diff.on("data", (chunk) => {
431487
]
432488
});
433489

434-
diff.on("finish", () => console.log("The full diff is available"))
490+
diff.on("finish", () => console.log("Your data has been processed. The full diff is available."))
435491
diff.on("error", (err) => console.log(err))
436492
```
493+
437494
<hr/>
438495

439496
### isEqual()

0 commit comments

Comments
 (0)