X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=4306d8872e0114928481769feb6bb6daf5ff603e;hb=84168fc3d6347e325764d384a3d1a8e132f497b1;hp=0271754f1c00b5df6d24b90501cc4b7d4c831ee0;hpb=268a74bb051fcbbad532fd833f0d8fd2b33b6c64;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 0271754f..4306d887 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -3,6 +3,7 @@ import util from 'node:util'; import clone from 'just-clone'; +// import { Constants } from './internal'; import { Constants } from './Constants'; import { WebSocketCloseEventStatusString } from '../types'; @@ -185,6 +186,14 @@ export class Utils { return clone(object); } + public static hasOwnProp(object: unknown, property: PropertyKey): boolean { + return Utils.isObject(object) && Object.hasOwn(object as object, property); + } + + public static isCFEnvironment(): boolean { + return !Utils.isNullOrUndefined(process.env.VCAP_APPLICATION); + } + public static isIterable(obj: T): boolean { return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false; } @@ -214,17 +223,11 @@ export class Utils { } public static isEmptyArray(object: unknown | unknown[]): boolean { - if (Array.isArray(object) && object.length === 0) { - return true; - } - return false; + return Array.isArray(object) && object.length === 0; } public static isNotEmptyArray(object: unknown | unknown[]): boolean { - if (Array.isArray(object) && object.length > 0) { - return true; - } - return false; + return Array.isArray(object) && object.length > 0; } public static isEmptyObject(obj: object): boolean { @@ -246,9 +249,9 @@ export class Utils { * @param retryNumber - the number of retries that have already been attempted * @returns delay in milliseconds */ - public static exponentialDelay(retryNumber = 0): number { + public static exponentialDelay(retryNumber = 0, maxDelayRatio = 0.2): number { const delay = Math.pow(2, retryNumber) * 100; - const randomSum = delay * 0.2 * Utils.secureRandom(); // 0-20% of the delay + const randomSum = delay * maxDelayRatio * Utils.secureRandom(); // 0-20% of the delay return delay + randomSum; }