refactor: cleanup type casting
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / Utils.ts
CommitLineData
2eb6954d
JB
1import util from 'node:util';
2
32de5a57
LM
3export default class Utils {
4 // STATE
5 public static isUndefined(value: unknown): boolean {
6 return typeof value === 'undefined';
7 }
8
9 public static ifUndefined<T>(value: T | undefined, isValue: T): T {
10 if (Utils.isUndefined(value) === true) return isValue;
11 return value as T;
12 }
13
afb26ef1 14 public static isIterable<T>(obj: T): boolean {
32de5a57
LM
15 if (obj === null || obj === undefined) {
16 return false;
17 }
3c8798b1 18 return typeof (obj as unknown as Iterable<T>)[Symbol.iterator] === 'function';
32de5a57
LM
19 }
20
21 // public static ifNotIterableDo<T>(obj: T, cb: () => void): void {
22 // if (this.isIterable(obj) === false) cb();
23 // }
24
2eb6954d
JB
25 public static isPromisePending(promise: Promise<unknown>): boolean {
26 return util.inspect(promise).includes('pending');
27 }
28
32de5a57
LM
29 public static async promiseWithTimeout<T>(
30 promise: Promise<T>,
31 timeoutMs: number,
32 timeoutError: Error,
33 timeoutCallback: () => void = () => {
34 /* This is intentional */
35 }
36 ): Promise<T> {
37 // Create a timeout promise that rejects in timeout milliseconds
38 const timeoutPromise = new Promise<never>((_, reject) => {
39 setTimeout(() => {
2eb6954d
JB
40 if (Utils.isPromisePending(promise)) {
41 timeoutCallback();
42 // FIXME: The original promise shall be canceled
43 }
32de5a57
LM
44 reject(timeoutError);
45 }, timeoutMs);
46 });
47
48 // Returns a race between timeout promise and the passed promise
49 return Promise.race<T>([promise, timeoutPromise]);
50 }
51
52 // FUNCTIONAL
53 // public static compose<T>(...fns: ((arg: T) => T)[]): (x: T) => T {
54 // return (x: T) => fns.reduceRight((y, fn) => fn(y), x);
55 // }
56}