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)
}
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)
}
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 = <I extends CloneableData, O extends CloneableData = I>(
- value: I,
- formatKey?: FormatKey,
- refs: Map<I, O> = new Map<I, O>()
-): 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<string, CloneableData> = {}
- 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 = <T>(object: T): T => {
- return deepClone(object as CloneableData) as T
+ return structuredClone<T>(object)
}
/**
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
}
return getRandomValues(new Uint32Array(1))[0] / 0x100000000
}
-export const JSONStringifyWithMapSupport = (
- object:
- | Record<string, unknown>
+export const JSONStringify = <
+ T extends
+ | JsonType
| Array<Record<string, unknown>>
- | Map<unknown, unknown>
- | ProtocolResponse,
- space?: string | number
-): string => {
+ | Set<Record<string, unknown>>
+ | Map<string, Record<string, unknown>>
+>(
+ object: T,
+ space?: string | number,
+ mapFormat?: MapStringifyFormat
+ ): string => {
return JSON.stringify(
object,
(_, value: Record<string, unknown>) => {
if (value instanceof Map) {
- return {
- dataType: 'Map',
- value: [...value]
+ switch (mapFormat) {
+ case MapStringifyFormat.object:
+ return { ...Object.fromEntries<Map<string, Record<string, unknown>>>(value.entries()) }
+ case MapStringifyFormat.array:
+ default:
+ return [...value]
}
+ } else if (value instanceof Set) {
+ return [...value] as JsonType[]
}
return value
},