1 import util from
'node:util';
3 export default class Utils
{
5 public static isUndefined(value
: unknown
): boolean {
6 return typeof value
=== 'undefined';
9 public static ifUndefined
<T
>(value
: T
| undefined, isValue
: T
): T
{
10 if (Utils
.isUndefined(value
) === true) return isValue
;
14 public static isIterable
<T
>(obj
: T
): boolean {
15 if (obj
=== null || obj
=== undefined) {
18 return typeof (obj
as unknown
as Iterable
<T
>)[Symbol
.iterator
] === 'function';
21 // public static ifNotIterableDo<T>(obj: T, cb: () => void): void {
22 // if (this.isIterable(obj) === false) cb();
25 public static isPromisePending(promise
: Promise
<unknown
>): boolean {
26 return util
.inspect(promise
).includes('pending');
29 public static async promiseWithTimeout
<T
>(
33 timeoutCallback
: () => void = () => {
34 /* This is intentional */
37 // Create a timeout promise that rejects in timeout milliseconds
38 const timeoutPromise
= new Promise
<never>((_
, reject
) => {
40 if (Utils
.isPromisePending(promise
)) {
42 // FIXME: The original promise shall be canceled
48 // Returns a race between timeout promise and the passed promise
49 return Promise
.race
<T
>([promise
, timeoutPromise
]);
53 // public static compose<T>(...fns: ((arg: T) => T)[]): (x: T) => T {
54 // return (x: T) => fns.reduceRight((y, fn) => fn(y), x);