Cleanups.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
index 85b2d4748d91c52daab0dfbcff866f63fdedbbac..216790bc368bc0e7664810138391d0e41129aa13 100644 (file)
@@ -1,7 +1,7 @@
 const {v4: uuid} = require('uuid');
 
 class Utils {
-  static generateGUID() {
+  static generateUUID() {
     return uuid();
   }
 
@@ -74,30 +74,49 @@ 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) {
+    let result = false;
+    // Check boolean
+    if (value) {
+      // Check the type
+      if (typeof value === 'boolean') {
+        // Already a boolean
+        result = value;
+      } else {
+        // Convert
+        result = (value === 'true');
+      }
+    }
+    return result;
   }
 
   static getRandomInt(max, min) {
@@ -111,6 +130,21 @@ class Utils {
     const date = new Date();
     return date.toISOString().substr(0, 19) + prefixString;
   }
+
+  static objectHasOwnProperty(object, property) {
+    return Object.prototype.hasOwnProperty.call(object, property);
+  }
+
+  static cloneJSonDocument(jsonDocument) {
+    return JSON.parse(JSON.stringify(jsonDocument));
+  }
+
+  static isUndefined(value) {
+    if (typeof value === 'undefined') {
+      return true;
+    }
+    return false;
+  }
 }
 
 module.exports = Utils;