fix: ensure dates in ISO string format are properly converted to Date
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 87ea9a2cc1f96c5ad19aab5e6a20760c126f8489..e8c52603fbc238ce2d7f5278844ed662a93ea30c 100644 (file)
@@ -29,7 +29,7 @@ export const validateUUID = (uuid: string): boolean => {
 }
 
 export const sleep = async (milliSeconds: number): Promise<NodeJS.Timeout> => {
-  return await new Promise<NodeJS.Timeout>((resolve) =>
+  return await new Promise<NodeJS.Timeout>(resolve =>
     setTimeout(resolve as () => void, milliSeconds)
   )
 }
@@ -66,7 +66,7 @@ export const formatDurationSeconds = (duration: number): string => {
 }
 
 // More efficient time validation function than the one provided by date-fns
-export const isValidTime = (date: unknown): boolean => {
+export const isValidTime = (date: Date | number | undefined): boolean => {
   if (typeof date === 'number') {
     return !isNaN(date)
   } else if (isDate(date)) {
@@ -154,7 +154,7 @@ export const getRandomFloat = (max = Number.MAX_VALUE, min = 0): number => {
 
 export const getRandomInteger = (max = Constants.MAX_RANDOM_INTEGER, min = 0): number => {
   max = Math.floor(max)
-  if (min != null && min !== 0) {
+  if (min !== 0) {
     min = Math.ceil(min)
     return Math.floor(randomInt(min, max + 1))
   }
@@ -175,7 +175,7 @@ export const roundTo = (numberValue: number, scale: number): number => {
 }
 
 export const getRandomFloatRounded = (max = Number.MAX_VALUE, min = 0, scale = 2): number => {
-  if (min != null && min !== 0) {
+  if (min !== 0) {
     return roundTo(getRandomFloat(max, min), scale)
   }
   return roundTo(getRandomFloat(max), scale)
@@ -203,7 +203,7 @@ export const getRandomFloatFluctuatedRounded = (
 }
 
 export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] => {
-  return timeSeries.map((timeSeriesItem) => timeSeriesItem.value)
+  return timeSeries.map(timeSeriesItem => timeSeriesItem.value)
 }
 
 export const isObject = (item: unknown): boolean => {
@@ -294,7 +294,7 @@ export const isNotEmptyArray = (object: unknown): boolean => {
 }
 
 export const isEmptyObject = (obj: object): boolean => {
-  if (obj?.constructor !== Object) {
+  if (obj.constructor !== Object) {
     return false
   }
   // Iterates over the keys of an object, if
@@ -371,8 +371,8 @@ export const getWebSocketCloseEventStatusString = (code: number): string => {
     }
   }
   if (
-    WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString] !==
-    undefined
+    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+    WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString] != null
   ) {
     return WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString]
   }
@@ -395,6 +395,7 @@ export const once = <T, A extends any[], R>(
 ): ((...args: A) => R) => {
   let result: R
   return (...args: A) => {
+    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
     if (fn != null) {
       result = fn.apply<T, A, R>(context, args)
       ;(fn as unknown as undefined) = (context as unknown as undefined) = undefined