X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=226f54c4df5abfa54386737d2e0ffe0ed7e40ac1;hb=1b0147cae119f5b519cffd8cdf2a733a3df20b11;hp=d6fe05d42c7c282d3fe5e2dcafa18eff5cb694e1;hpb=fd1ee77ce04cf81c52a5e66408b148a9847eb4ec;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index d6fe05d4..226f54c4 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -17,7 +17,7 @@ export default class Utils { return new Date(milliSeconds).toISOString().substr(11, 8); } - static removeExtraEmptyLines(tab): void { + static removeExtraEmptyLines(tab: string[]): void { // Start from the end for (let i = tab.length - 1; i > 0; i--) { // Two consecutive empty lines? @@ -102,7 +102,13 @@ export default class Utils { } static roundTo(number: number, scale: number): number { - return Utils.convertToFloat(number.toFixed(scale)); + const roundPower = Math.pow(10, scale); + return Math.round(number * roundPower) / roundPower; + } + + static truncTo(number: number, scale: number): number { + const truncPower = Math.pow(10, scale); + return Math.trunc(number * truncPower) / truncPower; } static getRandomFloatRounded(max: number, min = 0, scale = 2): number { @@ -117,12 +123,8 @@ export default class Utils { return date.toLocaleString() + prefixString; } - static objectHasOwnProperty(object, property): boolean { - return Object.prototype.hasOwnProperty.call(object, property); - } - - static cloneObject(object) { - return JSON.parse(JSON.stringify(object)); + static cloneObject(object: T): T { + return JSON.parse(JSON.stringify(object)) as T; } static isIterable(obj): boolean { @@ -176,4 +178,14 @@ export default class Utils { } static insertAt = (str: string, subStr: string, pos: number): string => `${str.slice(0, pos)}${subStr}${str.slice(pos)}`; + + /** + * @param {number} [retryNumber=0] + * @return {number} - delay in milliseconds + */ + static exponentialDelay(retryNumber = 0): number { + const delay = Math.pow(2, retryNumber) * 100; + const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay + return delay + randomSum; + } }