docs: fix README.md formatting
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 6f4517bee1b5aaf213698fbb976c421660ef1f6d..ae27035ff079c028f30bee9884d5b2de5521c18a 100644 (file)
@@ -14,7 +14,11 @@ import {
 } from 'date-fns'
 
 import { Constants } from './Constants.js'
-import { type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js'
+import {
+  type EmptyObject,
+  type TimestampedData,
+  WebSocketCloseEventStatusString
+} from '../types/index.js'
 
 export const logPrefix = (prefixString = ''): string => {
   return `${new Date().toLocaleString()}${prefixString}`
@@ -77,9 +81,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
@@ -236,7 +240,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
@@ -257,11 +261,22 @@ 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
   }