-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathutil.ts
More file actions
336 lines (296 loc) · 10.1 KB
/
util.ts
File metadata and controls
336 lines (296 loc) · 10.1 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { v4 as uuid4, v5 as uuid5 } from 'uuid';
import type { Input, InputData, InputId, InputValue, NodeSchema, OutputId } from './common-types';
export const EMPTY_ARRAY: readonly never[] = [];
export const EMPTY_SET: ReadonlySet<never> = new Set<never>();
export const EMPTY_MAP: ReadonlyMap<never, never> = new Map<never, never>();
export const EMPTY_OBJECT: Readonly<Record<string, never>> = Object.freeze({});
export const noop = () => {};
export const assertNever = (value: never): never => {
throw new Error(`Unreachable code path. The value ${String(value)} is invalid.`);
};
export const assertType: <T>(_: T) => void = noop;
export const isReadonlyArray = Array.isArray as (value: unknown) => value is readonly unknown[];
export const isNotNullish = <T>(value: T | null | undefined): value is T => value != null;
export const deepCopy = <T>(value: T): T => JSON.parse(JSON.stringify(value)) as T;
export const findLastIndex = <T>(
array: readonly T[],
predicate: (value: T, index: number, obj: readonly T[]) => unknown
): number => {
for (let i = array.length - 1; i >= 0; i -= 1) {
if (predicate(array[i], i, array)) {
return i;
}
}
return -1;
};
export const findLast = <T>(
array: readonly T[],
predicate: (value: T, index: number, obj: readonly T[]) => unknown
): T | undefined => {
const index = findLastIndex(array, predicate);
if (index === -1) return undefined;
return array[index];
};
export interface ParsedSourceHandle {
nodeId: string;
outputId: OutputId;
}
export interface ParsedTargetHandle {
nodeId: string;
inputId: InputId;
}
export const parseSourceHandle = (handle: string): ParsedSourceHandle => {
return {
nodeId: handle.substring(0, 36),
outputId: Number(handle.substring(37)) as OutputId,
};
};
export const parseTargetHandle = (handle: string): ParsedTargetHandle => {
return {
nodeId: handle.substring(0, 36),
inputId: Number(handle.substring(37)) as InputId,
};
};
export const stringifySourceHandle = (handle: ParsedSourceHandle): string =>
`${handle.nodeId}-${handle.outputId}`;
export const stringifyTargetHandle = (handle: ParsedTargetHandle): string =>
`${handle.nodeId}-${handle.inputId}`;
export const createUniqueId = () => uuid4();
export const deriveUniqueId = (input: string) =>
uuid5(input, '48f168a5-48dc-48b3-a7c7-2c3eedb08602');
export const lazy = <T>(fn: () => T): (() => T) => {
let hasValue = false;
let value: T;
return () => {
if (hasValue) return value;
value = fn();
hasValue = true;
return value;
};
};
// eslint-disable-next-line @typescript-eslint/ban-types
export const lazyKeyed = <K extends object, T extends {} | null>(
fn: (key: K) => T
): ((key: K) => T) => {
const cache = new WeakMap<K, T>();
return (key) => {
let value = cache.get(key);
if (value === undefined) {
value = fn(key);
cache.set(key, value);
}
return value;
};
};
export const cacheLast = <K extends NonNullable<unknown>, T>(
fn: (arg: K) => T
): ((arg: K) => T) => {
let lastArg: K | undefined;
let lastValue: T = undefined as T;
return (arg: K): T => {
if (lastArg === arg) return lastValue;
lastValue = fn(arg);
lastArg = arg;
return lastValue;
};
};
export interface CacheOptions {
readonly maxSize?: number;
}
export const cached = <K, T extends NonNullable<unknown>>(
fn: (arg: K) => T,
{ maxSize = 100 }: CacheOptions = {}
): ((arg: K) => T) => {
const cache = new Map<K, T>();
return (arg: K): T => {
let c = cache.get(arg);
if (c === undefined) {
if (cache.size >= maxSize && cache.size > 0) {
const firstKey = cache.keys().next().value as K;
cache.delete(firstKey);
}
c = fn(arg);
cache.set(arg, c);
}
return c;
};
};
export const debounce = (fn: () => void, delay: number): (() => void) => {
let id: NodeJS.Timeout | undefined;
return () => {
if (id !== undefined) clearTimeout(id);
id = setTimeout(fn, delay);
};
};
export const areApproximatelyEqual = (a: number, b: number): boolean => Math.abs(a - b) < 1e-12;
export const sameNumber = (a: number, b: number): boolean =>
a === b || (Number.isNaN(a) && Number.isNaN(b));
// eslint-disable-next-line no-nested-ternary
export const binaryCompare = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
export type Comparator<T> = (a: T, b: T) => number;
export const compareSequences = <T>(
a: readonly T[],
b: readonly T[],
compare: Comparator<T>
): number => {
if (a.length !== b.length) return a.length - b.length;
for (let i = 0; i < a.length; i += 1) {
const r = compare(a[i], b[i]);
if (r !== 0) return r;
}
return 0;
};
/**
* Sorts numbers in the order:
* 1. -Infinity
* 2. Negative real numbers. E.g. -2
* 3. -0.0
* 4. 0.0
* 5. Positive real numbers. E.g. 2
* 6. Infinity
* 7. NaN
*/
export const compareNumber = (a: number, b: number): number => {
if (a === 0 && b === 0) {
// compare -0 and 0
return compareNumber(1 / a, 1 / b);
}
if (Number.isFinite(a) && Number.isFinite(b)) {
return a - b;
}
if (sameNumber(a, b)) return 0;
if (Number.isNaN(a)) return +1;
if (Number.isNaN(b)) return -1;
return a - b;
};
type WithType<S, T extends string> = S extends { readonly type: T } ? S : never;
export type Visitors<State extends { readonly type: string }, R> = {
[K in State['type']]: (state: WithType<State, K>) => R;
};
export const visitByType = <State extends { readonly type: string }, R>(
state: State,
visitors: Visitors<State, R>
): R => {
const v = (visitors as Record<string, unknown>)[state.type] as (state: State) => R;
return v(state);
};
/**
* Tries to topologically sort the given graph. If the graph contains cycles, `undefined` will be
* returned.
*/
export const topologicalSort = <T>(
allNodes: Iterable<T>,
getOut: (node: T) => Iterable<T>
): T[] | undefined => {
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
const unmarked = new Set(allNodes);
const permanentMark = new Set<T>();
const tempMark = new Set<T>();
let cyclic = false;
const result: T[] = [];
const visit = (node: T): void => {
if (permanentMark.has(node)) return;
if (tempMark.has(node)) {
cyclic = true;
return;
}
unmarked.delete(node);
tempMark.add(node);
for (const out of getOut(node)) {
visit(out);
}
tempMark.delete(node);
permanentMark.add(node);
result.push(node);
};
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (!cyclic && unmarked.size > 0) {
const [first] = unmarked;
visit(first);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (cyclic) return undefined;
return result.reverse();
};
export const isEndingNode = (schema: NodeSchema) => {
return !schema.outputs.some((i) => i.hasHandle) && schema.inputs.length > 0;
};
export const delay = (ms: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
export const mapInputValues = <T>(schema: NodeSchema, getValue: (inputId: InputId) => T): T[] =>
schema.inputs.map((input) => getValue(input.id));
export const stopPropagation = (event: { readonly stopPropagation: () => void }): void => {
event.stopPropagation();
};
export const joinEnglish = (list: readonly string[], conj: 'and' | 'or' = 'and'): string => {
if (list.length === 0) throw new Error('Cannot join empty list');
if (list.length === 1) return list[0];
if (list.length === 2) return `${list[0]} ${conj} ${list[1]}`;
return `${list.slice(0, -1).join(', ')}, ${conj} ${list[list.length - 1]}`;
};
export const capitalize = (string: string): string =>
string.charAt(0).toUpperCase() + string.slice(1);
export const fixRoundingError = (n: number): number => {
if (!Number.isFinite(n)) return n;
const expS = n.toExponential(15);
if (/0{6}[0-3]\de[+-]\d+$/i.test(expS)) {
return Number(n.toExponential(12));
}
if (Number.isInteger(n)) return n;
const s = String(n);
if (/(?:9{6}[6-9]|0{6}[0-3])\d$/.test(s)) {
return Number(n.toPrecision(12));
}
return n;
};
export const getInputValue = <T extends NonNullable<InputValue>>(
inputId: InputId,
inputData: InputData
): T | undefined => {
return (inputData[inputId] ?? undefined) as T | undefined;
};
export const isAutoInput = (input: Input): boolean =>
input.kind === 'generic' && input.optional && !input.hasHandle;
export const getDefaultValue = <I extends Input>(
input: I
): undefined | (I extends { readonly def: unknown } ? NonNullable<I['def']> : never) => {
if ('def' in input) {
return (input.def ?? undefined) as never;
}
return undefined;
};
export const escapeRegExp = (string: string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
export function groupBy<T, K extends keyof T>(iter: Iterable<T>, key: K): Map<T[K], T[]>;
export function groupBy<T, K>(iter: Iterable<T>, selector: (item: T) => K): Map<K, T[]>;
// eslint-disable-next-line prefer-arrow-functions/prefer-arrow-functions
export function groupBy<T>(
iter: Iterable<T>,
key: keyof T | ((item: T) => unknown)
): Map<unknown, T[]> {
const map = new Map<unknown, T[]>();
if (typeof key === 'function') {
for (const item of iter) {
const k = key(item);
let list = map.get(k);
if (list === undefined) {
list = [];
map.set(k, list);
}
list.push(item);
}
} else {
for (const item of iter) {
const k = item[key];
let list = map.get(k);
if (list === undefined) {
list = [];
map.set(k, list);
}
list.push(item);
}
}
return map;
}