X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=9a06c64ed5658e01e7409bb38ae95baaba61be5c;hb=f223daa52d37f83d11ef1b4b6864eb8b549652a4;hp=bc9a8f436a8c1e7c8b60457f817e9467a6c96933;hpb=87bcd3b41c269bdb32468c766dca8d307689e8c2;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index bc9a8f43..9a06c64e 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,8 @@ import { minutesToSeconds, secondsToMilliseconds } from 'date-fns' +import type { CircularBuffer } from 'mnemonist' +import { is } from 'rambda' import { type JsonType, @@ -19,7 +21,6 @@ import { type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js' -import { Constants } from './Constants.js' export const logPrefix = (prefixString = ''): string => { return `${new Date().toLocaleString()}${prefixString}` @@ -112,7 +113,7 @@ export const convertToInt = (value: unknown): number => { } let changedValue: number = value as number if (typeof value === 'string') { - changedValue = parseInt(value) + changedValue = Number.parseInt(value) } if (isNaN(changedValue)) { throw new Error(`Cannot convert to integer: '${String(value)}'`) @@ -126,7 +127,7 @@ export const convertToFloat = (value: unknown): number => { } let changedValue: number = value as number if (typeof value === 'string') { - changedValue = parseFloat(value) + changedValue = Number.parseFloat(value) } if (isNaN(changedValue)) { throw new Error(`Cannot convert to float: '${String(value)}'`) @@ -153,21 +154,12 @@ export const getRandomFloat = (max = Number.MAX_VALUE, min = 0): number => { if (max < min) { throw new RangeError('Invalid interval') } - if (max - min === Infinity) { + if (max - min === Number.POSITIVE_INFINITY) { throw new RangeError('Invalid interval') } 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. @@ -209,8 +201,8 @@ export const getRandomFloatFluctuatedRounded = ( ) } -export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] => { - return timeSeries.map(timeSeriesItem => timeSeriesItem.value) +export const extractTimeSeriesValues = (timeSeries: CircularBuffer): number[] => { + return (timeSeries.toArray() as TimestampedData[]).map(timeSeriesItem => timeSeriesItem.value) } export const clone = (object: T): T => { @@ -225,11 +217,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 => { @@ -287,15 +279,17 @@ 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()) } + return { + ...Object.fromEntries>>(value.entries()) + } case MapStringifyFormat.array: default: return [...value] } - } else if (value instanceof Set) { + } else if (is(Set, value)) { return [...value] as JsonType[] } return value @@ -334,6 +328,9 @@ export const getWebSocketCloseEventStatusString = (code: number): string => { } export const isArraySorted = (array: T[], compareFn: (a: T, b: T) => number): boolean => { + if (array.length <= 1) { + return true + } for (let index = 0; index < array.length - 1; ++index) { if (compareFn(array[index], array[index + 1]) > 0) { return false