Skip to content

Commit 7fe7a84

Browse files
committed
chore: test stream-list-diff
1 parent aaf5f3f commit 7fe7a84

File tree

2 files changed

+425
-39
lines changed

2 files changed

+425
-39
lines changed

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

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
StreamReferences,
77
} from "@models/stream";
88
import { LIST_STATUS } from "@models/list";
9-
import { isEqual, isObject } from "@lib/utils";
9+
import { isObject } from "@lib/utils";
1010
import { Emitter, EventEmitter, StreamEvent } from "./emitter";
1111

1212
function outputDiffChunk<T extends Record<string, unknown>>(
@@ -31,6 +31,8 @@ function outputDiffChunk<T extends Record<string, unknown>>(
3131
const output = chunks;
3232
chunks = [];
3333
return emitter.emit(StreamEvent.Data, output);
34+
} else {
35+
return;
3436
}
3537
}
3638
return emitter.emit(StreamEvent.Data, [chunk]);
@@ -73,56 +75,53 @@ function isValidChunkSize(
7375
chunksSize: ListStreamOptions["chunksSize"],
7476
): boolean {
7577
if (!chunksSize) return true;
76-
const x = String(Math.sign(chunksSize));
77-
return x !== "-1" && x !== "NaN";
78+
const sign = String(Math.sign(chunksSize));
79+
return sign !== "-1" && sign !== "NaN";
7880
}
7981

8082
function isDataValid<T extends Record<string, unknown>>(
8183
data: T,
8284
referenceProperty: ReferenceProperty<T>,
83-
emitter: Emitter<T>,
8485
listType: "prevList" | "nextList",
85-
): boolean {
86+
): { isValid: boolean; message?: string } {
8687
if (!isObject(data)) {
87-
emitter.emit(
88-
StreamEvent.Error,
89-
new Error(
90-
`Your ${listType} must only contain valid objects. Found ${data}`,
91-
),
92-
);
93-
return false;
88+
return {
89+
isValid: false,
90+
message: `Your ${listType} must only contain valid objects. Found '${data}'`,
91+
};
9492
}
9593
if (!Object.hasOwn(data, referenceProperty)) {
96-
emitter.emit(
97-
StreamEvent.Error,
98-
new Error(
99-
`The reference property ${String(referenceProperty)} is not available in all the objects of your ${listType}.`,
100-
),
101-
);
102-
return false;
94+
return {
95+
isValid: false,
96+
message: `The reference property '${String(referenceProperty)}' is not available in all the objects of your ${listType}.`,
97+
};
10398
}
104-
return true;
99+
return {
100+
isValid: true,
101+
message: "",
102+
};
105103
}
106104

107105
function getDiffChunks<T extends Record<string, unknown>>(
108-
prevList: T[],
109-
nextList: T[],
106+
prevList: T[] = [],
107+
nextList: T[] = [],
110108
referenceProperty: ReferenceProperty<T>,
111109
emitter: Emitter<T>,
112110
options: ListStreamOptions = DEFAULT_LIST_STREAM_OPTIONS,
113-
) {
111+
): void {
114112
if (!isValidChunkSize(options?.chunksSize)) {
115113
return emitter.emit(
116114
StreamEvent.Error,
117115
new Error(
118-
`The chunk size can't be negative. You entered the value ${options.chunksSize}`,
116+
`The chunk size can't be negative. You entered the value '${options.chunksSize}'`,
119117
),
120118
);
121119
}
122-
if (!prevList && !nextList) {
123-
return [];
120+
if (prevList.length === 0 && nextList.length === 0) {
121+
return emitter.emit(StreamEvent.Finish);
124122
}
125-
if (!prevList) {
123+
const handleDiffChunk = outputDiffChunk<T>(emitter);
124+
if (prevList.length === 0) {
126125
const nextDiff = formatSingleListStreamDiff(
127126
nextList as T[],
128127
false,
@@ -136,11 +135,12 @@ function getDiffChunks<T extends Record<string, unknown>>(
136135
);
137136
emitter.emit(StreamEvent.Finish);
138137
}
139-
return nextDiff?.forEach((data, i) =>
138+
nextDiff?.forEach((data, i) =>
140139
handleDiffChunk(data, i === nextDiff.length - 1, options),
141140
);
141+
return emitter.emit(StreamEvent.Finish);
142142
}
143-
if (!nextList) {
143+
if (nextList.length === 0) {
144144
const prevDiff = formatSingleListStreamDiff(
145145
prevList as T[],
146146
true,
@@ -154,17 +154,22 @@ function getDiffChunks<T extends Record<string, unknown>>(
154154
);
155155
emitter.emit(StreamEvent.Finish);
156156
}
157-
return prevDiff?.forEach((data, i) =>
157+
prevDiff?.forEach((data, i) =>
158158
handleDiffChunk(data, i === prevDiff.length - 1, options),
159159
);
160+
return emitter.emit(StreamEvent.Finish);
160161
}
161162
const listsReferences: StreamReferences<T> = new Map();
162-
const handleDiffChunk = outputDiffChunk<T>(emitter);
163163
for (let i = 0; i < prevList.length; i++) {
164164
const data = prevList[i];
165165
if (data) {
166-
const isValid = isDataValid(data, referenceProperty, emitter, "prevList");
166+
const { isValid, message } = isDataValid(
167+
data,
168+
referenceProperty,
169+
"prevList",
170+
);
167171
if (!isValid) {
172+
emitter.emit(StreamEvent.Error, new Error(message));
168173
emitter.emit(StreamEvent.Finish);
169174
break;
170175
}
@@ -176,10 +181,15 @@ function getDiffChunks<T extends Record<string, unknown>>(
176181
}
177182

178183
for (let i = 0; i < nextList.length; i++) {
179-
const data = prevList[i];
184+
const data = nextList[i];
180185
if (data) {
181-
const isValid = isDataValid(data, referenceProperty, emitter, "nextList");
186+
const { isValid, message } = isDataValid(
187+
data,
188+
referenceProperty,
189+
"nextList",
190+
);
182191
if (!isValid) {
192+
emitter.emit(StreamEvent.Error, new Error(message));
183193
emitter.emit(StreamEvent.Finish);
184194
break;
185195
}
@@ -207,10 +217,11 @@ function getDiffChunks<T extends Record<string, unknown>>(
207217

208218
let streamedChunks = 0;
209219
const totalChunks = listsReferences.size;
220+
210221
for (const data of listsReferences.values()) {
211222
streamedChunks++;
212223
const isLastChunk = totalChunks === streamedChunks;
213-
if (!data.nextIndex) {
224+
if (typeof data.nextIndex === "undefined") {
214225
handleDiffChunk(
215226
{
216227
previousValue: prevList[data.prevIndex],
@@ -226,17 +237,17 @@ function getDiffChunks<T extends Record<string, unknown>>(
226237
} else {
227238
const prevData = prevList[data.prevIndex];
228239
const nextData = nextList[data.nextIndex];
229-
const isDataEqual = isEqual(prevData, nextData);
230-
const indexDiff = data.prevIndex - data.nextIndex;
240+
const isDataEqual = JSON.stringify(prevData) === JSON.stringify(nextData);
241+
const indexDiff = data.nextIndex - data.prevIndex;
231242
if (isDataEqual) {
232243
if (indexDiff === 0) {
233244
handleDiffChunk(
234245
{
235246
previousValue: prevList[data.prevIndex],
236247
currentValue: nextList[data.nextIndex],
237-
prevIndex: null,
248+
prevIndex: data.prevIndex,
238249
newIndex: data.nextIndex,
239-
indexDiff: null,
250+
indexDiff: 0,
240251
status: LIST_STATUS.EQUAL,
241252
},
242253
isLastChunk,
@@ -274,6 +285,7 @@ function getDiffChunks<T extends Record<string, unknown>>(
274285
}
275286
}
276287
}
288+
277289
return emitter.emit(StreamEvent.Finish);
278290
}
279291

0 commit comments

Comments
 (0)