X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=bac9705da1ba8e37f67418f86aebbc630529595e;hb=480fecf6f69bc342772aa161c4452e2b544e12a4;hp=5fb5f62573448fb3bab45174e4b1afb14f4acc90;hpb=1895299db899eb53db7fb1615b82624f806017e8;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 5fb5f625..bac9705d 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -3,17 +3,17 @@ import util from 'node:util'; import clone from 'just-clone'; -import Constants from './Constants'; -import { WebSocketCloseEventStatusString } from '../types/WebSocket'; +import { Constants } from './internal'; +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(); @@ -26,7 +26,7 @@ export default class Utils { } public static async sleep(milliSeconds: number): Promise { - return new Promise(resolve => setTimeout(resolve as () => void, milliSeconds)); + return new Promise((resolve) => setTimeout(resolve as () => void, milliSeconds)); } public static formatDurationMilliSeconds(duration: number): string { @@ -121,13 +121,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,7 +185,7 @@ export default class Utils { return clone(object); } - public static isIterable>(obj: T): boolean { + public static isIterable(obj: T): boolean { return !Utils.isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false; } @@ -193,26 +194,37 @@ 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)) { + 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 {