refactor: refine .cfignore
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 9d2b92fa4239856b0c064d062168dc5cb121a93d..abfac5e4873c98af8a4afa225ed1568fa4c9cec8 100644 (file)
@@ -14,7 +14,12 @@ import {
 } from 'date-fns'
 
 import { Constants } from './Constants.js'
-import { type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js'
+import {
+  type EmptyObject,
+  type ProtocolResponse,
+  type TimestampedData,
+  WebSocketCloseEventStatusString
+} from '../types/index.js'
 
 export const logPrefix = (prefixString = ''): string => {
   return `${new Date().toLocaleString()}${prefixString}`
@@ -97,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)
   }
@@ -206,62 +211,26 @@ 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 = <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.valueOf()) 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 structuredClone<T>(object)
 }
 
-export const clone = <T>(object: T): T => {
-  return deepClone(object as CloneableData) as T
+/**
+ * Detects whether the given value is an asynchronous function or not.
+ *
+ * @param fn - Unknown value.
+ * @returns `true` if `fn` was an asynchronous function, otherwise `false`.
+ * @internal
+ */
+export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise<unknown> => {
+  return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction'
 }
 
 export const isObject = (value: unknown): value is object => {
   return value != null && typeof value === 'object' && !Array.isArray(value)
 }
 
-export const isEmptyObject = (object: object): object is Record<string, never> => {
+export const isEmptyObject = (object: object): object is EmptyObject => {
   if (object.constructor !== Object) {
     return false
   }
@@ -328,8 +297,12 @@ export const secureRandom = (): number => {
 }
 
 export const JSONStringifyWithMapSupport = (
-  object: Record<string, unknown> | Array<Record<string, unknown>> | Map<unknown, unknown>,
-  space?: number
+  object:
+  | Record<string, unknown>
+  | Array<Record<string, unknown>>
+  | Map<unknown, unknown>
+  | ProtocolResponse,
+  space?: string | number
 ): string => {
   return JSON.stringify(
     object,