Add Power.Active.Import measurand support.
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.js
index f25f48b2b8a81d03b78a72a2c56dd6a4fd5ebf6e..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,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
@@ -119,14 +99,29 @@ class Utils {
     return result;
   }
 
-  static getRandomInt(max, min) {
+  static getRandomFloat(max, min = 0) {
+    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(Math.random() * (max - min + 1) + min);
+    }
+    return Math.floor(Math.random() * max + 1);
+  }
+
+  static roundTo(number, scale) {
+    return Utils.convertToFloat(number.toFixed(scale));
+  }
+
+  static getRandomFloatRounded(max, min = 0, scale = 2) {
     if (min) {
-      return Math.floor((Math.random() * (max - min)) + min);
+      return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
     }
-    return Math.floor((Math.random() * max));
+    return Utils.roundTo(Utils.getRandomFloat(max), scale);
   }
 
-  static basicFormatLog(prefixString = '') {
+  static logPrefix(prefixString = '') {
     const date = new Date();
     return date.toLocaleString() + prefixString;
   }
@@ -135,16 +130,54 @@ 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) {
+    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;
   }
-}
 
-module.exports = Utils;
+  static isEmptyArray(object) {
+    if (Array.isArray(object) && object.length > 0) {
+      return false;
+    }
+    return true;
+  }
+
+  static isEmptyObject(obj) {
+    return !Object.keys(obj).length;
+  }
+}