X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=4c4c20f234ff02faf4f79974db7913858b0c0dd7;hb=61877a2e1a2cf976a5e5f7f37828950d8aca9af5;hp=cff63715719b07d254b045c133a9be2170e01291;hpb=89448b6160fe75bd2dc10e409ba6c0988768a154;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index cff63715..4c4c20f2 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -13,23 +13,26 @@ import { secondsToMilliseconds } from 'date-fns' -import { Constants } from './Constants.js' import { type EmptyObject, - type ProtocolResponse, + type JsonType, + MapStringifyFormat, type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js' +import { Constants } from './Constants.js' export const logPrefix = (prefixString = ''): string => { return `${new Date().toLocaleString()}${prefixString}` } -export const generateUUID = (): string => { +export const generateUUID = (): `${string}-${string}-${string}-${string}-${string}` => { return randomUUID() } -export const validateUUID = (uuid: string): boolean => { +export const validateUUID = ( + uuid: `${string}-${string}-${string}-${string}-${string}` +): uuid is `${string}-${string}-${string}-${string}-${string}` => { return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(uuid) } @@ -102,13 +105,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) } @@ -211,55 +214,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) } /** @@ -310,7 +266,7 @@ export const isNotEmptyString = (value: unknown): value is string => { return isString(value) && value.trim().length > 0 } -export const isEmptyArray = (value: unknown): value is never[] => { +export const isEmptyArray = (value: unknown): value is [] => { return Array.isArray(value) && value.length === 0 } @@ -343,22 +299,30 @@ export const secureRandom = (): number => { return getRandomValues(new Uint32Array(1))[0] / 0x100000000 } -export const JSONStringifyWithMapSupport = ( - object: - | Record +export const JSONStringify = < + T extends + | JsonType | Array> - | Map - | ProtocolResponse, - space?: string | number -): string => { + | Set> + | Map> +>( + object: T, + space?: string | number, + mapFormat?: MapStringifyFormat + ): string => { return JSON.stringify( object, (_, value: Record) => { if (value instanceof Map) { - return { - dataType: 'Map', - value: [...value] + switch (mapFormat) { + case MapStringifyFormat.object: + return { ...Object.fromEntries>>(value.entries()) } + case MapStringifyFormat.array: + default: + return [...value] } + } else if (value instanceof Set) { + return [...value] as JsonType[] } return value },