X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=7195ce09ecfff2bfab6ed5c5016b876190830c8b;hb=8262cbb563cdea574e23b6e846f2658997ee2852;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..7195ce09 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,15 @@ 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)) { - 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 {