Apply dependencies update
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 719200fa93e40999d71b6553469ec4f98498ec6e..8dda95a699b362c66c5bb195e96de4abf30e39c1 100644 (file)
@@ -16,7 +16,9 @@ export default class Utils {
   }
 
   public static validateUUID(uuid: string): boolean {
-    return /\/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$\/i/.test(uuid);
+    return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(
+      uuid
+    );
   }
 
   public static async sleep(milliSeconds: number): Promise<NodeJS.Timeout> {
@@ -68,9 +70,10 @@ export default class Utils {
     if (Number.isSafeInteger(value)) {
       return value as number;
     }
-    // Check
+    if (typeof value === 'number') {
+      changedValue = Math.trunc(value);
+    }
     if (Utils.isString(value)) {
-      // Create Object
       changedValue = parseInt(value as string);
     }
     return changedValue;
@@ -81,9 +84,7 @@ export default class Utils {
     if (!value) {
       return 0;
     }
-    // Check
     if (Utils.isString(value)) {
-      // Create Object
       changedValue = parseFloat(value as string);
     }
     return changedValue;
@@ -91,15 +92,17 @@ export default class Utils {
 
   public static convertToBoolean(value: unknown): boolean {
     let result = false;
-    // Check boolean
     if (value) {
       // Check the type
       if (typeof value === 'boolean') {
-        // Already a boolean
         result = value;
-      } else {
-        // Convert
-        result = value === 'true';
+      } else if (
+        Utils.isString(value) &&
+        ((value as string).toLowerCase() === 'true' || value === '1')
+      ) {
+        result = true;
+      } else if (typeof value === 'number' && value === 1) {
+        result = true;
       }
     }
     return result;
@@ -184,11 +187,11 @@ export default class Utils {
     return value == null ? true : false;
   }
 
-  public static isEmptyArray(object: unknown): boolean {
+  public static isEmptyArray(object: unknown | unknown[]): boolean {
     if (!Array.isArray(object)) {
-      return false;
+      return true;
     }
-    if ((object as unknown[]).length > 0) {
+    if (object.length > 0) {
       return false;
     }
     return true;