X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=73e5e3f96e421b7f2bbec4bb1021ab58f5c09d47;hb=7429b96a712aec23847317058bbdceada172cbc3;hp=e4bc6a7b723f89ff593fff7dee1f9aba97ce2a74;hpb=035742f7433c00edbd0259966e7af6a1cfa2f0a1;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index e4bc6a7b..73e5e3f9 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,48 +40,48 @@ export default class Utils { } } - static convertToDate(value): Date { + static convertToDate(value: unknown): Date { // Check if (!value) { - return value; + return value as Date; } // Check Type if (!(value instanceof Date)) { - return new Date(value); + return new Date(value as string); } return value; } - static convertToInt(value): number { - let changedValue = value; + static convertToInt(value: unknown): number { + let changedValue: number = value as number; if (!value) { return 0; } if (Number.isSafeInteger(value)) { - return value; + return value as number; } // Check - if (typeof value === 'string') { + if (Utils.isString(value)) { // Create Object - changedValue = parseInt(value); + changedValue = parseInt(value as string); } return changedValue; } - static convertToFloat(value): number { - let changedValue = value; + static convertToFloat(value: unknown): number { + let changedValue: number = value as number; if (!value) { return 0; } // Check - if (typeof value === 'string') { + if (Utils.isString(value)) { // Create Object - changedValue = parseFloat(value); + changedValue = parseFloat(value as string); } return changedValue; } - static convertToBoolean(value): boolean { + static convertToBoolean(value: unknown): 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: unknown): 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: unknown): boolean { return typeof value === 'string'; } - static isUndefined(value): boolean { + static isUndefined(value: unknown): boolean { return typeof value === 'undefined'; } - static isNullOrUndefined(value): boolean { + static isNullOrUndefined(value: unknown): 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: unknown): boolean { if (!object) { return true; } @@ -176,15 +183,15 @@ export default class Utils { return true; } - static isEmptyObject(obj): boolean { + static isEmptyObject(obj: Record): boolean { return !Object.keys(obj).length; } static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`; /** - * @param {number} [retryNumber=0] - * @returns {number} - delay in milliseconds + * @param [retryNumber=0] + * @returns delay in milliseconds */ static exponentialDelay(retryNumber = 0): number { const delay = Math.pow(2, retryNumber) * 100; @@ -192,6 +199,12 @@ export default class Utils { return delay + randomSum; } + /** + * Convert websocket error code to human readable string message + * + * @param code websocket error code + * @returns human readable string message + */ static getWebSocketCloseEventStatusString(code: number): string { if (code >= 0 && code <= 999) { return '(Unused)';