build(ui): switch to vite
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / Utils.ts
1 export default class Utils {
2 // STATE
3 public static isUndefined(value: unknown): boolean {
4 return typeof value === 'undefined';
5 }
6
7 public static ifUndefined<T>(value: T | undefined, isValue: T): T {
8 if (Utils.isUndefined(value) === true) return isValue;
9 return value as T;
10 }
11
12 public static isIterable<T>(obj: T): boolean {
13 if (obj === null || obj === undefined) {
14 return false;
15 }
16 return typeof (obj as any)[Symbol.iterator] === 'function';
17 }
18
19 // public static ifNotIterableDo<T>(obj: T, cb: () => void): void {
20 // if (this.isIterable(obj) === false) cb();
21 // }
22
23 public static async promiseWithTimeout<T>(
24 promise: Promise<T>,
25 timeoutMs: number,
26 timeoutError: Error,
27 timeoutCallback: () => void = () => {
28 /* This is intentional */
29 }
30 ): Promise<T> {
31 // Create a timeout promise that rejects in timeout milliseconds
32 const timeoutPromise = new Promise<never>((_, reject) => {
33 setTimeout(() => {
34 timeoutCallback();
35 reject(timeoutError);
36 }, timeoutMs);
37 });
38
39 // Returns a race between timeout promise and the passed promise
40 return Promise.race<T>([promise, timeoutPromise]);
41 }
42
43 // FUNCTIONAL
44 // public static compose<T>(...fns: ((arg: T) => T)[]): (x: T) => T {
45 // return (x: T) => fns.reduceRight((y, fn) => fn(y), x);
46 // }
47 }