Closed DoneDeal0 closed 1 month ago
import { streamListDiff } from "@donedeal0/superdiff";
Streams the diff of two object lists, ideal for large lists and maximum performance.
Input
prevList: Record<string, unknown>[], nextList: Record<string, unknown>[], referenceProperty: keyof Record<string, unknown>, options: { showOnly?: ("added" | "deleted" | "moved" | "updated" | "equal")[], // [] by default chunksSize?: number, // // 0 by default considerMoveAsUpdate? boolean; // false by default }
prevList
nextList
referenceProperty
id
options
chunksSize
0
10
showOnly
["added", "equal"]
considerMoveAsUpdate
true
moved
updated
Output
The objects diff are grouped in arrays - called chunks - and are consumed thanks to an event listener. You have access to 3 events:
chunks
data
finish
error
interface StreamListener<T extends Record<string, unknown>> { on<E extends keyof EmitterEvents<T>>( event: E, listener: Listener<EmitterEvents<T>[E]>, ): this; } type EmitterEvents<T extends Record<string, unknown>> = { data: [StreamListDiff<T>[]]; error: [Error]; finish: []; }; type StreamListDiff<T extends Record<string, unknown>> = { currentValue: T | null; previousValue: T | null; prevIndex: number | null; newIndex: number | null; indexDiff: number | null; status: "added" | "deleted" | "moved" | "updated" | "equal"; };
const diff = streamListDiff( [ - { id: 1, name: "Item 1" }, { id: 2, name: "Item 2" }, { id: 3, name: "Item 3" } ], [ + { id: 0, name: "Item 0" }, { id: 2, name: "Item 2" }, + { id: 3, name: "Item Three" }, ], "id", { chunksSize: 2 } );
diff.on("data", (chunk) => { // first chunk received (2 object diffs) [ + { + previousValue: null, + currentValue: { id: 0, name: 'Item 0' }, + prevIndex: null, + newIndex: 0, + indexDiff: null, + status: 'added' + }, - { - previousValue: { id: 1, name: 'Item 1' }, - currentValue: null, - prevIndex: 0, - newIndex: null, - indexDiff: null, - status: 'deleted' - } ] // second chunk received (2 object diffs) [ { previousValue: { id: 2, name: 'Item 2' }, currentValue: { id: 2, name: 'Item 2' }, prevIndex: 1, newIndex: 1, indexDiff: 0, status: 'equal' }, + { + previousValue: { id: 3, name: 'Item 3' }, + currentValue: { id: 3, name: 'Item Three' }, + prevIndex: 2, + newIndex: 2, + indexDiff: 0, + status: 'updated' + }, ] }); diff.on("finish", () => console.log("The full diff is available")) diff.on("error", (err) => console.log(err))
SWC
@models
@lib
README.md
:tada: This PR is included in version 2.1.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
NEW FEATURE
streamListDiff()
Streams the diff of two object lists, ideal for large lists and maximum performance.
FORMAT
Input
prevList
: the original object list.nextList
: the new object list.referenceProperty
: a common property in all the objects of your lists (e.g.id
).options
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).showOnly
gives you the option to return only the values whose status you are interested in (e.g.["added", "equal"]
).considerMoveAsUpdate
: if set totrue
amoved
value will be considered asupdated
.Output
The objects diff are grouped in arrays - called
chunks
- and are consumed thanks to an event listener. You have access to 3 events:data
: to be notified when a new chunk of object diffs is available.finish
: to be notified when the stream is complete.error
: to be notified of an error during the stream.USAGE
Input
Output
ADDITIONAL FIXES
SWC
to speed up the tests.@models
,@lib
).README.md
.