X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=abfac5e4873c98af8a4afa225ed1568fa4c9cec8;hb=0b1828224edf798044ef54672ddfc69598cd99a5;hp=ae27035ff079c028f30bee9884d5b2de5521c18a;hpb=1e2ec4a6612809e178b5237bf6302562ba3c0512;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index ae27035f..abfac5e4 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -16,6 +16,7 @@ import { import { Constants } from './Constants.js' import { type EmptyObject, + type ProtocolResponse, type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js' @@ -101,13 +102,13 @@ export const convertToInt = (value: unknown): number => { if (value == null) { return 0 } - let changedValue: number = value as number if (Number.isSafeInteger(value)) { return value as number } if (typeof value === 'number') { return Math.trunc(value) } + let changedValue: number = value as number if (isString(value)) { changedValue = parseInt(value) } @@ -210,55 +211,8 @@ export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] return timeSeries.map(timeSeriesItem => timeSeriesItem.value) } -type CloneableData = - | number - | string - | boolean - | null - | undefined - | Date - | CloneableData[] - | { [key: string]: CloneableData } - -type FormatKey = (key: string) => string - -const deepClone = ( - value: I, - formatKey?: FormatKey, - refs: Map = new Map() -): O => { - const ref = refs.get(value) - if (ref !== undefined) { - return ref - } - if (Array.isArray(value)) { - const clone: CloneableData[] = [] - refs.set(value, clone as O) - for (let i = 0; i < value.length; i++) { - clone[i] = deepClone(value[i], formatKey, refs) - } - return clone as O - } - if (value instanceof Date) { - return new Date(value.getTime()) as O - } - if (typeof value !== 'object' || value === null) { - return value as unknown as O - } - const clone: Record = {} - refs.set(value, clone as O) - for (const key of Object.keys(value)) { - clone[typeof formatKey === 'function' ? formatKey(key) : key] = deepClone( - value[key], - formatKey, - refs - ) - } - return clone as O -} - export const clone = (object: T): T => { - return deepClone(object as CloneableData) as T + return structuredClone(object) } /** @@ -343,8 +297,12 @@ export const secureRandom = (): number => { } export const JSONStringifyWithMapSupport = ( - object: Record | Array> | Map, - space?: number + object: + | Record + | Array> + | Map + | ProtocolResponse, + space?: string | number ): string => { return JSON.stringify( object,