X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=38a212ca20c3f081cf2a9f6a2ec12719e49c70c1;hb=5aaca1e9d9b00c64f3b92752cebec6982c9c8dee;hp=25ac7634e28f3c4584d4fecda288be0de2f6387d;hpb=431c489c2b4b442ffad31573222b910780887495;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 25ac7634..38a212ca 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -4,6 +4,10 @@ import { WorkerProcessType } from '../types/Worker'; import { v4 as uuid } from 'uuid'; export default class Utils { + static logPrefix(prefixString = ''): string { + return new Date().toLocaleString() + prefixString; + } + static generateUUID(): string { return uuid(); } @@ -36,7 +40,7 @@ export default class Utils { } } - static convertToDate(value): Date { + static convertToDate(value: any): Date { // Check if (!value) { return value; @@ -48,7 +52,7 @@ export default class Utils { return value; } - static convertToInt(value): number { + static convertToInt(value: any): number { let changedValue = value; if (!value) { return 0; @@ -64,7 +68,7 @@ export default class Utils { return changedValue; } - static convertToFloat(value): number { + static convertToFloat(value: any): number { let changedValue = value; if (!value) { return 0; @@ -77,7 +81,7 @@ export default class Utils { return changedValue; } - static convertToBoolean(value): boolean { + static convertToBoolean(value: any): boolean { let result = false; // Check boolean if (value) { @@ -121,23 +125,26 @@ export default class Utils { return Utils.roundTo(Utils.getRandomFloat(max), scale); } - static logPrefix(prefixString = ''): string { - const date = new Date(); - return date.toLocaleString() + prefixString; + static getRandomFloatFluctuatedRounded(staticValue: number, fluctuationPercent: number, scale = 2): number { + if (fluctuationPercent === 0) { + return Utils.roundTo(staticValue, scale); + } + const fluctuationRatio = fluctuationPercent / 100; + return Utils.getRandomFloatRounded(staticValue * (1 + fluctuationRatio), staticValue * (1 - fluctuationRatio), scale); } static cloneObject(object: T): T { return JSON.parse(JSON.stringify(object)) as T; } - static isIterable(obj): boolean { + static isIterable(obj: T): boolean { if (obj) { return typeof obj[Symbol.iterator] === 'function'; } return false; } - static isEmptyJSon(document): boolean { + static isEmptyJSon(document: any): boolean { // Empty? if (!document) { return true; @@ -150,15 +157,15 @@ export default class Utils { return Object.keys(document).length === 0; } - static isString(value): boolean { + static isString(value: any): boolean { return typeof value === 'string'; } - static isUndefined(value): boolean { + static isUndefined(value: any): boolean { return typeof value === 'undefined'; } - static isNullOrUndefined(value): boolean { + static isNullOrUndefined(value: any): boolean { // eslint-disable-next-line no-eq-null, eqeqeq if (value == null) { return true; @@ -166,7 +173,7 @@ export default class Utils { return false; } - static isEmptyArray(object): boolean { + static isEmptyArray(object: any): boolean { if (!object) { return true; } @@ -176,7 +183,7 @@ export default class Utils { return true; } - static isEmptyObject(obj): boolean { + static isEmptyObject(obj: any): boolean { return !Object.keys(obj).length; }