X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=4306d8872e0114928481769feb6bb6daf5ff603e;hb=039211f91daa930e261a976c0c5ffbf729fbe922;hp=1f17fe8735dce9bb1b794075aefd318da22e06a9;hpb=602d810aef1eebef219b274fc5bfdd42c3ec819d;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 1f17fe87..4306d887 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -3,10 +3,11 @@ import util from 'node:util'; import clone from 'just-clone'; -import Constants from './Constants'; -import { WebSocketCloseEventStatusString } from '../types/WebSocket'; +// import { Constants } from './internal'; +import { Constants } from './Constants'; +import { WebSocketCloseEventStatusString } from '../types'; -export default class Utils { +export class Utils { private constructor() { // This is intentional } @@ -185,6 +186,14 @@ export default 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,13 +223,11 @@ export default class Utils { } public static isEmptyArray(object: unknown | unknown[]): boolean { - if (!Array.isArray(object)) { - return true; - } - if (object.length > 0) { - return false; - } - return true; + return Array.isArray(object) && object.length === 0; + } + + public static isNotEmptyArray(object: unknown | unknown[]): boolean { + return Array.isArray(object) && object.length > 0; } public static isEmptyObject(obj: object): boolean { @@ -242,9 +249,9 @@ export default 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; }