feat(ui): display supervision url in charging stations list
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 2472bf617621b87ef3752cc6000447918aa25300..cff63715719b07d254b045c133a9be2170e01291 100644 (file)
@@ -16,6 +16,7 @@ import {
 import { Constants } from './Constants.js'
 import {
   type EmptyObject,
+  type ProtocolResponse,
   type TimestampedData,
   WebSocketCloseEventStatusString
 } from '../types/index.js'
@@ -240,7 +241,7 @@ const deepClone = <I extends CloneableData, O extends CloneableData = I>(
     return clone as O
   }
   if (value instanceof Date) {
-    return new Date(value.valueOf()) as O
+    return new Date(value.getTime()) as O
   }
   if (typeof value !== 'object' || value === null) {
     return value as unknown as O
@@ -261,6 +262,17 @@ 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)
 }
@@ -332,8 +344,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,