X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.js;h=9d071a415faabcb5a086ce1a6ab765d8320736bc;hb=fee830211b36e79b9d89d4c0d689d02bc6da4156;hp=bd8c80368d82169aabc69a1dfca01781fb5007ce;hpb=560bcf5b8e2a406e7de3e7e330da36e93dd6eea9;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.js b/src/utils/Utils.js index bd8c8036..9d071a41 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -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; + } +}