fix(simulator): fix mocha tests
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 810267200fb1e3b1a4949918f264d8f57e085455..f701cea041ab831d1b1ff323123e02c60b69b11b 100644 (file)
@@ -3,10 +3,11 @@ 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
   }
@@ -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');
     }
-    const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff;
-    const sign = negative && randomPositiveFloat < 0.5 ? -1 : 1;
-    return sign * (randomPositiveFloat * (max - min) + min);
+    if (max - min === Infinity) {
+      throw new RangeError('Invalid interval');
+    }
+    return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min;
   }
 
   public static getRandomInteger(max = Constants.MAX_RANDOM_INTEGER, min = 0): number {
@@ -184,6 +186,14 @@ export default class Utils {
     return clone<T>(object);
   }
 
+  public static hasOwnProp(object: unknown, property: PropertyKey): boolean {
+    return Utils.isObject(object) && Object.hasOwn(object as object, property);
+  }
+
+  public static isCFEnvironment(): boolean {
+    return process.env.VCAP_APPLICATION !== undefined;
+  }
+
   public static isIterable<T>(obj: T): boolean {
     return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false;
   }
@@ -193,7 +203,14 @@ 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 {
@@ -202,17 +219,21 @@ export default class Utils {
 
   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)) {
+    if (Array.isArray(object) && object.length === 0) {
       return true;
     }
-    if (object.length > 0) {
-      return false;
+    return false;
+  }
+
+  public static isNotEmptyArray(object: unknown | unknown[]): boolean {
+    if (Array.isArray(object) && object.length > 0) {
+      return true;
     }
-    return true;
+    return false;
   }
 
   public static isEmptyObject(obj: object): boolean {