X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Futils%2FUtils.ts;h=f701cea041ab831d1b1ff323123e02c60b69b11b;hb=878e026c886468c5d488b75a6d3636abea6fc07c;hp=433891550f1a222ba474aa2dd83f5edf08f46842;hpb=5a2a53cfa256a32aa342d4dee48492118065e84a;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 43389155..f701cea0 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 } @@ -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'); } - const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff; - const sign = negative && randomPositiveFloat < 0.5 ? -1 : 1; - return sign * (randomPositiveFloat * (max - min) + min); + if (max - min === Infinity) { + throw new RangeError('Invalid interval'); + } + return (crypto.randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min; } public static getRandomInteger(max = Constants.MAX_RANDOM_INTEGER, min = 0): number { @@ -184,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 process.env.VCAP_APPLICATION !== undefined; + } + public static isIterable(obj: T): boolean { return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false; } @@ -209,17 +219,21 @@ export default class Utils { 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)) { + if (Array.isArray(object) && object.length === 0) { return true; } - if (object.length > 0) { - return false; + return false; + } + + public static isNotEmptyArray(object: unknown | unknown[]): boolean { + if (Array.isArray(object) && object.length > 0) { + return true; } - return true; + return false; } public static isEmptyObject(obj: object): boolean {