Add Power.Active.Import measurand support.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
index bd8c80368d82169aabc69a1dfca01781fb5007ce..9d071a415faabcb5a086ce1a6ab765d8320736bc 100644 (file)
@@ -1,6 +1,6 @@
-const {v4: uuid} = require('uuid');
+import {v4 as uuid} from 'uuid';
 
-class Utils {
+export default class Utils {
   static generateUUID() {
     return uuid();
   }
@@ -15,26 +15,6 @@ class Utils {
     return date.toISOString().substr(11, 8);
   }
 
-  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--) {
@@ -120,17 +100,14 @@ class Utils {
   }
 
   static getRandomFloat(max, min = 0) {
-    if (min) {
-      return Math.random() * (max - min + 1) + min;
-    }
-    return Math.random() * max + 1;
+    return Math.random() < 0.5 ? (1 - Math.random()) * (max - min) + min : Math.random() * (max - min) + min;
   }
 
   static getRandomInt(max, min = 0) {
     if (min) {
-      return Math.floor(Utils.getRandomFloat(max, min));
+      return Math.floor(Math.random() * (max - min + 1) + min);
     }
-    return Math.floor(Utils.getRandomFloat(max));
+    return Math.floor(Math.random() * max + 1);
   }
 
   static roundTo(number, scale) {
@@ -153,8 +130,28 @@ class Utils {
     return Object.prototype.hasOwnProperty.call(object, property);
   }
 
-  static cloneJSonDocument(jsonDocument) {
-    return JSON.parse(JSON.stringify(jsonDocument));
+  static cloneObject(object) {
+    return JSON.parse(JSON.stringify(object));
+  }
+
+  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) {
@@ -179,6 +176,8 @@ class Utils {
     }
     return true;
   }
-}
 
-module.exports = Utils;
+  static isEmptyObject(obj) {
+    return !Object.keys(obj).length;
+  }
+}