refactor: improve types testing types definition
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index 42af7c528e7ae1251838461a35512c3e73af5ce9..3827d0dde99c519d6efb9477ff39e60cfb3d18ab 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 isValidDate = (date: Date | number | undefined): date is Date | number => {
   if (typeof date === 'number') {
     return !isNaN(date)
   } else if (isDate(date)) {
@@ -76,8 +76,8 @@ export const isValidTime = (date: unknown): boolean => {
 }
 
 export const convertToDate = (
-  value: Date | string | number | null | undefined
-): Date | null | undefined => {
+  value: Date | string | number | undefined | null
+): Date | undefined | null => {
   if (value == null) {
     return value
   }
@@ -85,10 +85,8 @@ export const convertToDate = (
     return value
   }
   if (isString(value) || typeof value === 'number') {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     const valueToDate = new Date(value)
     if (isNaN(valueToDate.getTime())) {
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       throw new Error(`Cannot convert to date: '${value}'`)
     }
     return valueToDate
@@ -107,7 +105,7 @@ export const convertToInt = (value: unknown): number => {
     return Math.trunc(value)
   }
   if (isString(value)) {
-    changedValue = parseInt(value as string)
+    changedValue = parseInt(value)
   }
   if (isNaN(changedValue)) {
     throw new Error(`Cannot convert to integer: '${String(value)}'`)
@@ -121,7 +119,7 @@ export const convertToFloat = (value: unknown): number => {
   }
   let changedValue: number = value as number
   if (isString(value)) {
-    changedValue = parseFloat(value as string)
+    changedValue = parseFloat(value)
   }
   if (isNaN(changedValue)) {
     throw new Error(`Cannot convert to float: '${String(value)}'`)
@@ -135,7 +133,7 @@ export const convertToBoolean = (value: unknown): boolean => {
     // Check the type
     if (typeof value === 'boolean') {
       return value
-    } else if (isString(value) && ((value as string).toLowerCase() === 'true' || value === '1')) {
+    } else if (isString(value) && (value.toLowerCase() === 'true' || value === '1')) {
       result = true
     } else if (typeof value === 'number' && value === 1) {
       result = true
@@ -156,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))
   }
@@ -177,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)
@@ -205,10 +203,10 @@ 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 => {
+export const isObject = (item: unknown): item is object => {
   return item != null && typeof item === 'object' && !Array.isArray(item)
 }
 
@@ -264,39 +262,35 @@ export const cloneObject = <T>(object: T): T => {
 }
 
 export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => {
-  return isObject(object) && Object.hasOwn(object as object, property)
+  return isObject(object) && Object.hasOwn(object, property)
 }
 
 export const isCFEnvironment = (): boolean => {
   return env.VCAP_APPLICATION != null
 }
 
-export const isIterable = <T>(obj: T): boolean => {
-  return obj != null ? typeof obj[Symbol.iterator as keyof T] === 'function' : false
-}
-
-const isString = (value: unknown): boolean => {
+const isString = (value: unknown): value is string => {
   return typeof value === 'string'
 }
 
-export const isEmptyString = (value: unknown): boolean => {
-  return value == null || (isString(value) && (value as string).trim().length === 0)
+export const isEmptyString = (value: unknown): value is string | undefined | null => {
+  return value == null || (isString(value) && value.trim().length === 0)
 }
 
-export const isNotEmptyString = (value: unknown): boolean => {
-  return isString(value) && (value as string).trim().length > 0
+export const isNotEmptyString = (value: unknown): value is string => {
+  return isString(value) && value.trim().length > 0
 }
 
-export const isEmptyArray = (object: unknown): boolean => {
+export const isEmptyArray = (object: unknown): object is unknown[] => {
   return Array.isArray(object) && object.length === 0
 }
 
-export const isNotEmptyArray = (object: unknown): boolean => {
+export const isNotEmptyArray = (object: unknown): object is unknown[] => {
   return Array.isArray(object) && object.length > 0
 }
 
 export const isEmptyObject = (obj: object): boolean => {
-  if (obj?.constructor !== Object) {
+  if (obj.constructor !== Object) {
     return false
   }
   // Iterates over the keys of an object, if
@@ -373,8 +367,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]
   }
@@ -397,6 +391,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