-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.ts
More file actions
81 lines (76 loc) · 1.95 KB
/
Copy pathutils.ts
File metadata and controls
81 lines (76 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { isObject } from "@lib/utils";
import { IEmitter } from "@models/emitter";
import { ListType } from "@models/list";
import {
ListStreamOptions,
ReferenceKey,
StreamEvent,
StreamListDiff,
} from "@models/stream";
export function isValidChunkSize(
chunksSize: ListStreamOptions["chunksSize"],
): boolean {
if (!chunksSize) return true;
const sign = String(Math.sign(chunksSize));
return sign !== "-1" && sign !== "NaN";
}
export function isDataValid<T extends Record<string, unknown>>(
data: T,
referenceKey: ReferenceKey<T>,
listType: ListType,
): { isValid: boolean; message?: string } {
if (!isObject(data)) {
return {
isValid: false,
message: `Your ${listType} must only contain valid objects. Found '${data}'`,
};
}
if (!Object.hasOwn(data, referenceKey)) {
return {
isValid: false,
message: `The referenceKey '${String(referenceKey)}' is not available in all the objects of your ${listType}.`,
};
}
return {
isValid: true,
message: "",
};
}
export function outputDiffChunk<T extends Record<string, unknown>>(
emitter: IEmitter<T>,
) {
let chunks: StreamListDiff<T>[] = [];
function handleDiffChunk(
chunk: StreamListDiff<T>,
options: ListStreamOptions,
): void {
const showChunk = options?.showOnly
? options?.showOnly.includes(chunk.status)
: true;
if (!showChunk) {
return;
}
if ((options.chunksSize as number) > 0) {
chunks.push(chunk);
if (chunks.length >= (options.chunksSize as number)) {
const output = chunks;
chunks = [];
return emitter.emit(StreamEvent.Data, output);
} else {
return;
}
}
return emitter.emit(StreamEvent.Data, [chunk]);
}
function releaseLastChunks() {
if (chunks.length > 0) {
const output = chunks;
chunks = [];
return emitter.emit(StreamEvent.Data, output);
}
}
return {
handleDiffChunk,
releaseLastChunks,
};
}