X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=4306d8872e0114928481769feb6bb6daf5ff603e;hb=039211f91daa930e261a976c0c5ffbf729fbe922;hp=ca8f44af23d44ed91b15047428787edccdb0a9db;hpb=a1e5fe4e22aaee2d4c7535fc9b6998f0e5f9a8f3;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index ca8f44af..4306d887 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -3,17 +3,18 @@ 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 } - public static logPrefix(prefixString = ''): string { + public static logPrefix = (prefixString = ''): string => { return `${new Date().toLocaleString()}${prefixString}`; - } + }; public static generateUUID(): string { return crypto.randomUUID(); @@ -121,13 +122,14 @@ export default class Utils { return result; } - public static getRandomFloat(max = Number.MAX_VALUE, min = 0, negative = false): number { - if (max < min || max < 0 || min < 0) { + public static getRandomFloat(max = Number.MAX_VALUE, min = 0): number { + if (max < min) { + throw new RangeError('Invalid interval'); + } + if (max - min === Infinity) { throw new RangeError('Invalid interval'); } - const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff; - const sign = negative && randomPositiveFloat < 0.5 ? -1 : 1; - return sign * (randomPositiveFloat * (max - min) + min); + return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min; } public static getRandomInteger(max = Constants.MAX_RANDOM_INTEGER, min = 0): number { @@ -184,8 +186,16 @@ 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 obj ? typeof obj[Symbol.iterator] === 'function' : false; + return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false; } public static isString(value: unknown): boolean { @@ -193,26 +203,31 @@ export default class Utils { } public static isEmptyString(value: unknown): boolean { - return Utils.isString(value) && (value as string).trim().length === 0; + return ( + Utils.isNullOrUndefined(value) || + (Utils.isString(value) && (value as string).trim().length === 0) + ); + } + + public static isNotEmptyString(value: unknown): boolean { + return Utils.isString(value) && (value as string).trim().length > 0; } public static isUndefined(value: unknown): boolean { - return typeof value === 'undefined'; + return value === undefined; } public static isNullOrUndefined(value: unknown): boolean { // eslint-disable-next-line eqeqeq, no-eq-null - return value == null ? true : false; + return value == null; } 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 { @@ -234,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; }