fix: strict number check
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 5fb5f62573448fb3bab45174e4b1afb14f4acc90..058d175d0d7ebc3c3776205c6c5c9429570a2358 100644 (file)
@@ -3,17 +3,18 @@ import util from 'node:util';
 
 import clone from 'just-clone';
 
-import Constants from './Constants';
-import { WebSocketCloseEventStatusString } from '../types/WebSocket';
+// import { Constants } from './internal';
+import { Constants } from './Constants';
+import { WebSocketCloseEventStatusString } from '../types';
 
-export default class Utils {
+export class Utils {
   private constructor() {
     // This is intentional
   }
 
-  public static logPrefix(prefixString = ''): string {
+  public static logPrefix = (prefixString = ''): string => {
     return `${new Date().toLocaleString()}${prefixString}`;
-  }
+  };
 
   public static generateUUID(): string {
     return crypto.randomUUID();
@@ -26,7 +27,7 @@ export default class Utils {
   }
 
   public static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
-    return new Promise(resolve => setTimeout(resolve as () => void, milliSeconds));
+    return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds));
   }
 
   public static formatDurationMilliSeconds(duration: number): string {
@@ -121,13 +122,14 @@ export default class Utils {
     return result;
   }
 
-  public static getRandomFloat(max = Number.MAX_VALUE, min = 0, negative = false): number {
-    if (max < min || max < 0 || min < 0) {
+  public static getRandomFloat(max = Number.MAX_VALUE, min = 0): number {
+    if (max < min) {
+      throw new RangeError('Invalid interval');
+    }
+    if (max - min === Infinity) {
       throw new RangeError('Invalid interval');
     }
-    const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff;
-    const sign = negative && randomPositiveFloat < 0.5 ? -1 : 1;
-    return sign * (randomPositiveFloat * (max - min) + min);
+    return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min;
   }
 
   public static getRandomInteger(max = Constants.MAX_RANDOM_INTEGER, min = 0): number {
@@ -184,7 +186,15 @@ export default class Utils {
     return clone<T>(object);
   }
 
-  public static isIterable<T extends Iterable<T>>(obj: T): boolean {
+  public static hasOwnProp(object: unknown, property: PropertyKey): boolean {
+    return Utils.isObject(object) && Object.hasOwn(object as object, property);
+  }
+
+  public static isCFEnvironment(): boolean {
+    return !Utils.isNullOrUndefined(process.env.VCAP_APPLICATION);
+  }
+
+  public static isIterable<T>(obj: T): boolean {
     return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false;
   }
 
@@ -193,26 +203,31 @@ export default class Utils {
   }
 
   public static isEmptyString(value: unknown): boolean {
-    return Utils.isString(value) && (value as string).trim().length === 0;
+    return (
+      Utils.isNullOrUndefined(value) ||
+      (Utils.isString(value) && (value as string).trim().length === 0)
+    );
+  }
+
+  public static isNotEmptyString(value: unknown): boolean {
+    return Utils.isString(value) && (value as string).trim().length > 0;
   }
 
   public static isUndefined(value: unknown): boolean {
-    return typeof value === 'undefined';
+    return value === undefined;
   }
 
   public static isNullOrUndefined(value: unknown): boolean {
     // eslint-disable-next-line eqeqeq, no-eq-null
-    return value == null ? true : false;
+    return value == null;
   }
 
   public static isEmptyArray(object: unknown | unknown[]): boolean {
-    if (!Array.isArray(object)) {
-      return true;
-    }
-    if (object.length > 0) {
-      return false;
-    }
-    return true;
+    return Array.isArray(object) && object.length === 0;
+  }
+
+  public static isNotEmptyArray(object: unknown | unknown[]): boolean {
+    return Array.isArray(object) && object.length > 0;
   }
 
   public static isEmptyObject(obj: object): boolean {