Make statistics class a singleton.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
index 15ee30711ec2c3645626b0e6af071abd212c71aa..bd8c80368d82169aabc69a1dfca01781fb5007ce 100644 (file)
@@ -15,18 +15,6 @@ class Utils {
     return date.toISOString().substr(11, 8);
   }
 
-  static convertToDate(date) {
-    // Check
-    if (!date) {
-      return date;
-    }
-    // Check Type
-    if (!(date instanceof Date)) {
-      return new Date(date);
-    }
-    return date;
-  }
-
   static isIterable(obj) {
     if (obj) {
       return typeof obj[Symbol.iterator] === 'function';
@@ -63,6 +51,18 @@ class Utils {
     }
   }
 
+  static convertToDate(date) {
+    // Check
+    if (!date) {
+      return date;
+    }
+    // Check Type
+    if (!(date instanceof Date)) {
+      return new Date(date);
+    }
+    return date;
+  }
+
   static convertToObjectID(id) {
     let changedID = id;
     // Check
@@ -119,14 +119,32 @@ class Utils {
     return result;
   }
 
-  static getRandomInt(max, min) {
+  static getRandomFloat(max, min = 0) {
     if (min) {
-      return Math.floor((Math.random() * (max - min)) + min);
+      return Math.random() * (max - min + 1) + min;
     }
-    return Math.floor((Math.random() * max));
+    return Math.random() * max + 1;
   }
 
-  static basicFormatLog(prefixString = '') {
+  static getRandomInt(max, min = 0) {
+    if (min) {
+      return Math.floor(Utils.getRandomFloat(max, min));
+    }
+    return Math.floor(Utils.getRandomFloat(max));
+  }
+
+  static roundTo(number, scale) {
+    return Utils.convertToFloat(number.toFixed(scale));
+  }
+
+  static getRandomFloatRounded(max, min = 0, scale = 2) {
+    if (min) {
+      return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
+    }
+    return Utils.roundTo(Utils.getRandomFloat(max), scale);
+  }
+
+  static logPrefix(prefixString = '') {
     const date = new Date();
     return date.toLocaleString() + prefixString;
   }
@@ -139,8 +157,17 @@ class Utils {
     return JSON.parse(JSON.stringify(jsonDocument));
   }
 
+  static isString(value) {
+    return typeof value === 'string';
+  }
+
   static isUndefined(value) {
-    if (typeof value === 'undefined') {
+    return typeof value === 'undefined';
+  }
+
+  static isNullOrUndefined(value) {
+    // eslint-disable-next-line eqeqeq
+    if (value == null) {
       return true;
     }
     return false;