build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 95c36bf93c53b147cf77d2f29756f6891da529b0..12c206ff63849ec0303c37a7ce8c41fffc5c29bb 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}`
@@ -77,9 +82,9 @@ export const isValidDate = (date: Date | number | undefined): date is Date | num
 
 export const convertToDate = (
   value: Date | string | number | undefined | null
-): Date | undefined | null => {
+): Date | undefined => {
   if (value == null) {
-    return value
+    return undefined
   }
   if (isDate(value)) {
     return value
@@ -206,63 +211,40 @@ export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[]
   return timeSeries.map(timeSeriesItem => timeSeriesItem.value)
 }
 
-export const isObject = (item: unknown): item is object => {
-  return item != null && typeof item === 'object' && !Array.isArray(item)
+export const clone = <T>(object: T): T => {
+  return structuredClone<T>(object)
 }
 
-type CloneableData =
-  | number
-  | string
-  | boolean
-  | null
-  | undefined
-  | Date
-  | CloneableData[]
-  | { [key: string]: CloneableData }
+/**
+ * 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'
+}
 
-type FormatKey = (key: string) => string
+export const isObject = (value: unknown): value is object => {
+  return value != null && typeof value === 'object' && !Array.isArray(value)
+}
 
-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
+export const isEmptyObject = (object: object): object is EmptyObject => {
+  if (object.constructor !== Object) {
+    return false
   }
-  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
-    )
+  // Iterates over the keys of an object, if
+  // any exist, return false.
+  // eslint-disable-next-line no-unreachable-loop
+  for (const _ in object) {
+    return false
   }
-  return clone as O
-}
-
-export const clone = <T>(object: T): T => {
-  return deepClone(object as CloneableData) as T
+  return true
 }
 
-export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => {
-  return isObject(object) && Object.hasOwn(object, property)
+export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => {
+  return isObject(value) && Object.hasOwn(value, property)
 }
 
 export const isCFEnvironment = (): boolean => {
@@ -273,7 +255,7 @@ const isString = (value: unknown): value is string => {
   return typeof value === 'string'
 }
 
-export const isEmptyString = (value: unknown): value is string | undefined | null => {
+export const isEmptyString = (value: unknown): value is '' | undefined | null => {
   return value == null || (isString(value) && value.trim().length === 0)
 }
 
@@ -281,25 +263,12 @@ export const isNotEmptyString = (value: unknown): value is string => {
   return isString(value) && value.trim().length > 0
 }
 
-export const isEmptyArray = (object: unknown): object is unknown[] => {
-  return Array.isArray(object) && object.length === 0
-}
-
-export const isNotEmptyArray = (object: unknown): object is unknown[] => {
-  return Array.isArray(object) && object.length > 0
+export const isEmptyArray = (value: unknown): value is never[] => {
+  return Array.isArray(value) && value.length === 0
 }
 
-export const isEmptyObject = (obj: object): boolean => {
-  if (obj.constructor !== Object) {
-    return false
-  }
-  // Iterates over the keys of an object, if
-  // any exist, return false.
-  // eslint-disable-next-line no-unreachable-loop
-  for (const _ in obj) {
-    return false
-  }
-  return true
+export const isNotEmptyArray = (value: unknown): value is unknown[] => {
+  return Array.isArray(value) && value.length > 0
 }
 
 export const insertAt = (str: string, subStr: string, pos: number): string =>
@@ -328,11 +297,15 @@ export const secureRandom = (): number => {
 }
 
 export const JSONStringifyWithMapSupport = (
-  obj: 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(
-    obj,
+    object,
     (_, value: Record<string, unknown>) => {
       if (value instanceof Map) {
         return {