Fixes to connectors initializing handling.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
index a06d57ead326e3dc052417fe902b54a9036b04fa..3009ae560f8ab154bfcc5bcd30edad504a1b3b51 100644 (file)
@@ -1,7 +1,7 @@
 const {v4: uuid} = require('uuid');
 
 class Utils {
-  static generateGUID() {
+  static generateUUID() {
     return uuid();
   }
 
@@ -15,38 +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';
-    }
-    return false;
-  }
-
-  static isEmptyJSon(document) {
-    // Empty?
-    if (!document) {
-      return true;
-    }
-    // Check type
-    if (typeof document !== 'object') {
-      return true;
-    }
-    // Check
-    return Object.keys(document).length === 0;
-  }
-
   static removeExtraEmptyLines(tab) {
     // Start from the end
     for (let i = tab.length - 1; i > 0; i--) {
@@ -63,6 +31,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
@@ -74,30 +54,33 @@ class Utils {
     return changedID;
   }
 
-  static convertToInt(id) {
-    let changedID = id;
-    if (!id) {
+  static convertToInt(value) {
+    let changedValue = value;
+    if (!value) {
       return 0;
     }
+    if (Number.isSafeInteger(value)) {
+      return value;
+    }
     // Check
-    if (typeof id === 'string') {
+    if (typeof value === 'string') {
       // Create Object
-      changedID = parseInt(id);
+      changedValue = parseInt(value);
     }
-    return changedID;
+    return changedValue;
   }
 
-  static convertToFloat(id) {
-    let changedID = id;
-    if (!id) {
+  static convertToFloat(value) {
+    let changedValue = value;
+    if (!value) {
       return 0;
     }
     // Check
-    if (typeof id === 'string') {
+    if (typeof value === 'string') {
       // Create Object
-      changedID = parseFloat(id);
+      changedValue = parseFloat(value);
     }
-    return changedID;
+    return changedValue;
   }
 
   static convertToBoolean(value) {
@@ -116,16 +99,34 @@ class Utils {
     return result;
   }
 
-  static getRandomInt(max, min) {
+  static getRandomFloat(max, min = 0) {
+    if (min) {
+      return Math.random() * (max - min + 1) + min;
+    }
+    return Math.random() * max + 1;
+  }
+
+  static getRandomInt(max, min = 0) {
     if (min) {
-      return Math.floor((Math.random() * (max - min)) + min);
+      return Math.floor(Utils.getRandomFloat(max, min));
     }
-    return Math.floor((Math.random() * max));
+    return Math.floor(Utils.getRandomFloat(max));
+  }
+
+  static roundTo(number, scale) {
+    return Utils.convertToFloat(number.toFixed(scale));
   }
 
-  static basicFormatLog(prefixString = '') {
+  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.toISOString().substr(0, 19) + prefixString;
+    return date.toLocaleString() + prefixString;
   }
 
   static objectHasOwnProperty(object, property) {
@@ -135,6 +136,49 @@ class Utils {
   static cloneJSonDocument(jsonDocument) {
     return JSON.parse(JSON.stringify(jsonDocument));
   }
+
+  static isIterable(obj) {
+    if (obj) {
+      return typeof obj[Symbol.iterator] === 'function';
+    }
+    return false;
+  }
+
+  static isEmptyJSon(document) {
+    // Empty?
+    if (!document) {
+      return true;
+    }
+    // Check type
+    if (typeof document !== 'object') {
+      return true;
+    }
+    // Check
+    return Object.keys(document).length === 0;
+  }
+
+  static isString(value) {
+    return typeof value === 'string';
+  }
+
+  static isUndefined(value) {
+    return typeof value === 'undefined';
+  }
+
+  static isNullOrUndefined(value) {
+    // eslint-disable-next-line eqeqeq
+    if (value == null) {
+      return true;
+    }
+    return false;
+  }
+
+  static isEmptyArray(object) {
+    if (Array.isArray(object) && object.length > 0) {
+      return false;
+    }
+    return true;
+  }
 }
 
 module.exports = Utils;