Skip to content

Commit c2d80ea

Browse files
committed
chore: update jsdoc
1 parent a7b4182 commit c2d80ea

File tree

11 files changed

+71
-129
lines changed

11 files changed

+71
-129
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: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ I am grateful to the generous donors of **Superdiff**!
4040

4141
## FEATURES
4242

43-
**Superdiff** exports 6 functions:
43+
**Superdiff** exports 5 functions:
4444

4545
```ts
4646
// Returns a complete diff of two objects
@@ -52,9 +52,6 @@ getListDiff(prevList, nextList)
5252
// Streams the diff of two object lists, ideal for large lists and maximum performance
5353
streamListDiff(prevList, nextList, referenceProperty)
5454

55-
// Similar to streamListDiff, but for browser use
56-
streamListDiffClient(prevList, nextList, referenceProperty)
57-
5855
// Checks whether two values are equal
5956
isEqual(dataA, dataB)
6057

@@ -310,9 +307,9 @@ getListDiff(
310307

311308
```js
312309
// If you are in a server environment
313-
import { streamListDiff } from "@donedeal0/superdiff";
310+
import { streamListDiff } from "@donedeal0/superdiff/server";
314311
// If you are in a browser environment
315-
import { streamListDiffClient } from "@donedeal0/superdiff";
312+
import { streamListDiff } from "@donedeal0/superdiff/client";
316313
```
317314

318315
Streams the diff of two object lists, ideal for large lists and maximum performance.
@@ -321,12 +318,11 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
321318

322319
**Input**
323320

324-
#### streamListDiff (server)
321+
#### Server
325322

326323
> 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.
327324
328325
```ts
329-
// streamListDiff
330326
prevList: Readable | FilePath | Record<string, unknown>[],
331327
nextList: Readable | FilePath | Record<string, unknown>[],
332328
referenceProperty: keyof Record<string, unknown>,
@@ -337,7 +333,7 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
337333
}
338334
```
339335

340-
#### streamListDiffClient (browser)
336+
#### Browser
341337

342338
> 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.
343339
@@ -495,74 +491,6 @@ diff.on("finish", () => console.log("Your data has been processed. The full diff
495491
diff.on("error", (err) => console.log(err))
496492
```
497493

498-
**Using `fetch`**
499-
500-
A common use case would be to do a live diff against a stream, in order to avoid loading the entire dataset into memory. Here are two examples, for browser and server use:
501-
502-
Browser:
503-
```ts
504-
import { streamListDiffClient } from "@donedeal0/superdiff";
505-
506-
async function streamDiffFromAPI() {
507-
try {
508-
const response = await fetch("https://example.com/api/streaming-data");
509-
const reader = response.body.getReader();
510-
511-
const stream = new ReadableStream({
512-
async start(controller) {
513-
let result;
514-
while (!(result = await reader.read()).done) {
515-
controller.enqueue(result.value); // Push the next chunk into the stream
516-
}
517-
controller.close(); // Close the stream when done
518-
},
519-
});
520-
521-
const prevStream = [{ id: 1, name: "Joe" }, { id: 2, name: "Jane" }] // Some previous list or stream
522-
523-
const diff = streamListDiffClient(prevStream, stream, 'id', { chunksSize: 5 });
524-
diff.on("data", (diffChunk) => console.log(diffChunk));
525-
diff.on("finish", () => console.log("Stream diff complete"));
526-
} catch (err) {
527-
console.error(err);
528-
}
529-
}
530-
```
531-
532-
Server:
533-
534-
```ts
535-
import fetch from "node-fetch";
536-
import { Readable } from "stream";
537-
import { streamListDiff } from "@donedeal0/superdiff";
538-
539-
async function streamDiffFromAPI() {
540-
try {
541-
const response = await fetch("https://example.com/api/streaming-data");
542-
const reader = response.body.getReader();
543-
544-
const stream = new Readable({
545-
async read() {
546-
let result;
547-
while (!(result = await reader.read()).done) {
548-
this.push(result.value); // Push the next chunk into the stream
549-
}
550-
this.push(null); // Close the stream when done
551-
},
552-
});
553-
554-
const prevList = [{ id: 1, name: "Joe" }, { id: 2, name: "Jane" }]; // Some previous list or stream
555-
const prevListStream = Readable.from(prevList, { objectMode: true })
556-
const diff = streamListDiff(prevListStream, stream, 'id', { chunksSize: 5 });
557-
558-
diff.on("data", (diffChunk) => console.log(diffChunk));
559-
diff.on("finish", () => console.log("Stream diff complete"));
560-
} catch (err) {
561-
console.error(err);
562-
}
563-
}
564-
```
565-
566494
<hr/>
567495

568496
### isEqual()

src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { streamListDiff } from "./lib/stream-list-diff/client";

src/index.ts

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

src/lib/stream-list-diff/client/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isClient } from "@lib/utils";
21
import {
32
DataBuffer,
43
DEFAULT_LIST_STREAM_OPTIONS,
@@ -246,26 +245,21 @@ async function getValidClientStream<T extends Record<string, unknown>>(
246245

247246
/**
248247
* Streams the diff of two object lists
249-
* @param {Record<string, unknown>[]} prevList - The original object list.
250-
* @param {Record<string, unknown>[]} nextList - The new object list.
251-
* @param {ReferenceProperty<T>} referenceProperty - A common property in all the objects of your lists (e.g. `id`)
248+
* @param {ReadableStream | File | Record<string, unknown>[]} prevList - The original object list.
249+
* @param {ReadableStream | File | Record<string, unknown>[]} nextList - The new object list.
250+
* @param {string} referenceProperty - A common property in all the objects of your lists (e.g. `id`)
252251
* @param {ListStreamOptions} options - Options to refine your output.
253252
- `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).
254253
- `showOnly`: returns only the values whose status you are interested in. (e.g. `["added", "equal"]`)
255254
- `considerMoveAsUpdate`: if set to `true` a `moved` object will be considered as `updated`
256-
* @returns EventEmitter
255+
* @returns StreamListener
257256
*/
258-
export function streamListDiffClient<T extends Record<string, unknown>>(
257+
export function streamListDiff<T extends Record<string, unknown>>(
259258
prevList: ReadableStream<T> | File | T[],
260259
nextList: ReadableStream<T> | File | T[],
261260
referenceProperty: ReferenceProperty<T>,
262261
options: ListStreamOptions = DEFAULT_LIST_STREAM_OPTIONS,
263262
): StreamListener<T> {
264-
if (!isClient()) {
265-
throw new Error(
266-
"streamListDiffClient can only be used in a browser environment. Please use streamListDiff instead.",
267-
);
268-
}
269263
const emitter = new EventEmitter<EmitterEvents<T>>();
270264
setTimeout(async () => {
271265
try {

src/lib/stream-list-diff/client/stream-list-diff-client.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "blob-polyfill";
55
import { ReadableStream } from "web-streams-polyfill";
66
import { LIST_STATUS } from "@models/list";
77
import { StreamListDiff } from "@models/stream";
8-
import { streamListDiffClient } from ".";
8+
import { streamListDiff } from ".";
99
import prevListFile from "../../../mocks/prevList.json";
1010
import nextListFile from "../../../mocks/nextList.json";
1111

@@ -18,7 +18,7 @@ describe("data emission", () => {
1818
{ id: 1, name: "Item 1" },
1919
{ id: 2, name: "Item 2" },
2020
];
21-
const diff = streamListDiffClient([], nextList, "id", { chunksSize: 2 });
21+
const diff = streamListDiff([], nextList, "id", { chunksSize: 2 });
2222

2323
const expectedChunks = [
2424
{
@@ -53,7 +53,7 @@ describe("data emission", () => {
5353
{ id: 1, name: "Item 1" },
5454
{ id: 2, name: "Item 2" },
5555
];
56-
const diff = streamListDiffClient(prevList, [], "id", { chunksSize: 2 });
56+
const diff = streamListDiff(prevList, [], "id", { chunksSize: 2 });
5757

5858
const expectedChunks = [
5959
{
@@ -93,7 +93,7 @@ describe("data emission", () => {
9393
{ id: 2, name: "Item 2" },
9494
{ id: 3, name: "Item 3" },
9595
];
96-
const diff = streamListDiffClient(prevList, nextList, "id");
96+
const diff = streamListDiff(prevList, nextList, "id");
9797

9898
const expectedChunks = [
9999
[
@@ -164,7 +164,7 @@ describe("data emission", () => {
164164
{ id: 9, name: "Item 9" },
165165
{ id: 8, name: "Item 8" },
166166
];
167-
const diff = streamListDiffClient(prevList, nextList, "id", {
167+
const diff = streamListDiff(prevList, nextList, "id", {
168168
chunksSize: 5,
169169
});
170170

@@ -291,7 +291,7 @@ describe("data emission", () => {
291291
{ id: 5, name: "Item 5" },
292292
];
293293

294-
const diff = streamListDiffClient(prevList, nextList, "id", {
294+
const diff = streamListDiff(prevList, nextList, "id", {
295295
chunksSize: 150,
296296
});
297297

@@ -362,7 +362,7 @@ describe("data emission", () => {
362362
{ id: 3, name: "Item 3" },
363363
{ id: 5, name: "Item 5" },
364364
];
365-
const diff = streamListDiffClient(prevList, nextList, "id", {
365+
const diff = streamListDiff(prevList, nextList, "id", {
366366
chunksSize: 5,
367367
considerMoveAsUpdate: true,
368368
});
@@ -434,7 +434,7 @@ describe("data emission", () => {
434434
{ id: 3, name: "Item 3" },
435435
{ id: 5, name: "Item 5" },
436436
];
437-
const diff = streamListDiffClient(prevList, nextList, "id", {
437+
const diff = streamListDiff(prevList, nextList, "id", {
438438
chunksSize: 5,
439439
showOnly: ["added", "deleted"],
440440
});
@@ -522,7 +522,7 @@ describe("data emission", () => {
522522
{ id: 9, name: "Item 9" },
523523
{ id: 8, name: "Item 8" },
524524
];
525-
const diff = streamListDiffClient(prevList, nextList, "id", {
525+
const diff = streamListDiff(prevList, nextList, "id", {
526526
chunksSize: 5,
527527
});
528528

@@ -751,7 +751,7 @@ describe("input handling", () => {
751751
},
752752
});
753753

754-
const diff = streamListDiffClient(prevStream, nextStream, "id", {
754+
const diff = streamListDiff(prevStream, nextStream, "id", {
755755
chunksSize: 5,
756756
});
757757

@@ -775,7 +775,7 @@ describe("input handling", () => {
775775
type: "application/json",
776776
});
777777

778-
const diff = streamListDiffClient(prevFile, nextFile, "id", {
778+
const diff = streamListDiff(prevFile, nextFile, "id", {
779779
chunksSize: 5,
780780
});
781781

@@ -801,7 +801,7 @@ describe("input handling", () => {
801801
type: "application/json",
802802
});
803803

804-
const diff = streamListDiffClient(prevStream, nextFile, "id", {
804+
const diff = streamListDiff(prevStream, nextFile, "id", {
805805
chunksSize: 5,
806806
});
807807

@@ -824,7 +824,7 @@ describe("input handling", () => {
824824
},
825825
});
826826

827-
const diff = streamListDiffClient(prevStream, nextList, "id", {
827+
const diff = streamListDiff(prevStream, nextList, "id", {
828828
chunksSize: 5,
829829
});
830830

@@ -844,7 +844,7 @@ describe("input handling", () => {
844844
type: "application/json",
845845
});
846846

847-
const diff = streamListDiffClient(prevFile, nextList, "id", {
847+
const diff = streamListDiff(prevFile, nextList, "id", {
848848
chunksSize: 5,
849849
});
850850

@@ -863,7 +863,7 @@ describe("input handling", () => {
863863

864864
describe("finish event", () => {
865865
it("emits 'finish' event if no prevList nor nextList is provided", (done) => {
866-
const diff = streamListDiffClient([], [], "id");
866+
const diff = streamListDiff([], [], "id");
867867
diff.on("finish", () => done());
868868
});
869869
it("emits 'finish' event when all the chunks have been processed", (done) => {
@@ -875,7 +875,7 @@ describe("finish event", () => {
875875
{ id: 2, name: "Item 2" },
876876
{ id: 3, name: "Item 3" },
877877
];
878-
const diff = streamListDiffClient(prevList, nextList, "id");
878+
const diff = streamListDiff(prevList, nextList, "id");
879879
diff.on("finish", () => done());
880880
});
881881
});
@@ -893,7 +893,7 @@ describe("error event", () => {
893893
];
894894

895895
// @ts-expect-error prevList is invalid by design for the test
896-
const diff = streamListDiffClient(prevList, nextList, "id");
896+
const diff = streamListDiff(prevList, nextList, "id");
897897

898898
diff.on("error", (err) => {
899899
expect(err["message"]).toEqual(
@@ -915,7 +915,7 @@ describe("error event", () => {
915915
];
916916

917917
// @ts-expect-error nextList is invalid by design for the test
918-
const diff = streamListDiffClient(prevList, nextList, "id");
918+
const diff = streamListDiff(prevList, nextList, "id");
919919

920920
diff.on("error", (err) => {
921921
expect(err["message"]).toEqual(
@@ -932,7 +932,7 @@ describe("error event", () => {
932932
{ id: 2, name: "Item 2" },
933933
];
934934

935-
const diff = streamListDiffClient(prevList, nextList, "id");
935+
const diff = streamListDiff(prevList, nextList, "id");
936936

937937
diff.on("error", (err) => {
938938
expect(err["message"]).toEqual(
@@ -949,7 +949,7 @@ describe("error event", () => {
949949
];
950950
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
951951

952-
const diff = streamListDiffClient(prevList, nextList, "id");
952+
const diff = streamListDiff(prevList, nextList, "id");
953953

954954
diff.on("error", (err) => {
955955
expect(err["message"]).toEqual(
@@ -966,7 +966,7 @@ describe("error event", () => {
966966
];
967967
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
968968

969-
const diff = streamListDiffClient(prevList, nextList, "id", {
969+
const diff = streamListDiff(prevList, nextList, "id", {
970970
chunksSize: -3,
971971
});
972972

@@ -982,7 +982,7 @@ describe("error event", () => {
982982
const nextList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
983983

984984
// @ts-expect-error - prevList is invalid by design for the test
985-
const diff = streamListDiffClient({ name: "hello" }, nextList, "id");
985+
const diff = streamListDiff({ name: "hello" }, nextList, "id");
986986

987987
diff.on("error", (err) => {
988988
expect(err["message"]).toEqual(
@@ -995,7 +995,7 @@ describe("error event", () => {
995995
const prevList = [{ id: 1, name: "Item 1" }, { name: "Item 2" }];
996996

997997
// @ts-expect-error - nextList is invalid by design for the test
998-
const diff = streamListDiffClient(prevList, null, "id");
998+
const diff = streamListDiff(prevList, null, "id");
999999

10001000
diff.on("error", (err) => {
10011001
expect(err["message"]).toEqual(
@@ -1025,7 +1025,7 @@ describe("performance", () => {
10251025
nextList[20_000].value = "updated-value-20000"; // Another updated entry
10261026
nextList.push({ id: numEntries, value: `new-value-${numEntries}` }); // 1 added entry
10271027

1028-
const diffListener = streamListDiffClient<{ id: number; value: string }>(
1028+
const diffListener = streamListDiff<{ id: number; value: string }>(
10291029
prevList,
10301030
nextList,
10311031
"id",

0 commit comments

Comments
 (0)