X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=aeed9b0fa44b4a073bff682583c95695cbb954ac;hb=8a4f882a2486682b72195ad978ec1d81604b452b;hp=fc3cf9a9cb55fba343d3739d14fbd2766c456604;hpb=38ae4ce2dba0f31e0f21c20490be0e7d376ce47c;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index fc3cf9a9..aeed9b0f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -1,4 +1,4 @@ -import { getRandomValues, randomBytes, randomInt, randomUUID } from 'node:crypto' +import { getRandomValues, randomBytes, randomUUID } from 'node:crypto' import { env, nextTick } from 'node:process' import { @@ -12,6 +12,7 @@ import { minutesToSeconds, secondsToMilliseconds } from 'date-fns' +import { is } from 'rambda' import { type JsonType, @@ -19,7 +20,6 @@ import { type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js' -import { Constants } from './Constants.js' export const logPrefix = (prefixString = ''): string => { return `${new Date().toLocaleString()}${prefixString}` @@ -91,7 +91,7 @@ export const convertToDate = ( if (isDate(value)) { return value } - if (isString(value) || typeof value === 'number') { + if (typeof value === 'string' || typeof value === 'number') { const valueToDate = new Date(value) if (isNaN(valueToDate.getTime())) { throw new Error(`Cannot convert to date: '${value}'`) @@ -111,7 +111,7 @@ export const convertToInt = (value: unknown): number => { return Math.trunc(value) } let changedValue: number = value as number - if (isString(value)) { + if (typeof value === 'string') { changedValue = parseInt(value) } if (isNaN(changedValue)) { @@ -125,7 +125,7 @@ export const convertToFloat = (value: unknown): number => { return 0 } let changedValue: number = value as number - if (isString(value)) { + if (typeof value === 'string') { changedValue = parseFloat(value) } if (isNaN(changedValue)) { @@ -140,7 +140,7 @@ export const convertToBoolean = (value: unknown): boolean => { // Check the type if (typeof value === 'boolean') { return value - } else if (isString(value) && (value.toLowerCase() === 'true' || value === '1')) { + } else if (typeof value === 'string' && (value.toLowerCase() === 'true' || value === '1')) { result = true } else if (typeof value === 'number' && value === 1) { result = true @@ -159,15 +159,6 @@ export const getRandomFloat = (max = Number.MAX_VALUE, min = 0): number => { return (randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min } -export const getRandomInteger = (max = Constants.MAX_RANDOM_INTEGER, min = 0): number => { - max = Math.floor(max) - if (min !== 0) { - min = Math.ceil(min) - return Math.floor(randomInt(min, max + 1)) - } - return Math.floor(randomInt(max + 1)) -} - /** * Rounds the given number to the given scale. * The rounding is done using the "round half away from zero" method. @@ -225,11 +216,11 @@ export const clone = (object: T): T => { * @internal */ export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise => { - return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' + return is(Function, fn) && fn.constructor.name === 'AsyncFunction' } export const isObject = (value: unknown): value is object => { - return value != null && typeof value === 'object' && !Array.isArray(value) + return value != null && !Array.isArray(value) && is(Object, value) } export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => { @@ -240,12 +231,8 @@ export const isCFEnvironment = (): boolean => { return env.VCAP_APPLICATION != null } -const isString = (value: unknown): value is string => { - return typeof value === 'string' -} - export const isNotEmptyString = (value: unknown): value is string => { - return isString(value) && value.trim().length > 0 + return typeof value === 'string' && value.trim().length > 0 } export const isNotEmptyArray = (value: unknown): value is unknown[] => { @@ -291,7 +278,7 @@ export const JSONStringify = < return JSON.stringify( object, (_, value: Record) => { - if (value instanceof Map) { + if (is(Map, value)) { switch (mapFormat) { case MapStringifyFormat.object: return { ...Object.fromEntries>>(value.entries()) } @@ -299,7 +286,7 @@ export const JSONStringify = < default: return [...value] } - } else if (value instanceof Set) { + } else if (is(Set, value)) { return [...value] as JsonType[] } return value @@ -346,12 +333,6 @@ export const isArraySorted = (array: T[], compareFn: (a: T, b: T) => number): return true } -export const min = (...args: number[]): number => - args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) - -export const max = (...args: number[]): number => - args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) - export const throwErrorInNextTick = (error: Error): void => { nextTick(() => { throw error