build(simulator): switch to strict type checking
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index f15224a855b7e08e53e75e4221bb1009a80f19cd..8d51349a2357fbb9ea0b450dd3367f00f8c7406e 100644 (file)
@@ -4,7 +4,7 @@ import { inspect } from 'node:util';
 import clone from 'just-clone';
 
 import { Constants } from './Constants';
-import { WebSocketCloseEventStatusString } from '../types';
+import { type TimestampedData, WebSocketCloseEventStatusString } from '../types';
 
 export const logPrefix = (prefixString = ''): string => {
   return `${new Date().toLocaleString()}${prefixString}`;
@@ -16,7 +16,7 @@ export const generateUUID = (): string => {
 
 export const validateUUID = (uuid: string): boolean => {
   return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(
-    uuid
+    uuid,
   );
 };
 
@@ -50,7 +50,7 @@ export const formatDurationSeconds = (duration: number): string => {
 };
 
 export const convertToDate = (
-  value: Date | string | number | null | undefined
+  value: Date | string | number | null | undefined,
 ): Date | null | undefined => {
   if (isNullOrUndefined(value)) {
     return value as null | undefined;
@@ -59,7 +59,7 @@ export const convertToDate = (
     return value;
   }
   if (isString(value) || typeof value === 'number') {
-    return new Date(value);
+    return new Date(value!);
   }
   return null;
 };
@@ -79,6 +79,7 @@ export const convertToInt = (value: unknown): number => {
     changedValue = parseInt(value as string);
   }
   if (isNaN(changedValue)) {
+    // eslint-disable-next-line @typescript-eslint/no-base-to-string
     throw new Error(`Cannot convert to integer: ${value.toString()}`);
   }
   return changedValue;
@@ -93,6 +94,7 @@ export const convertToFloat = (value: unknown): number => {
     changedValue = parseFloat(value as string);
   }
   if (isNaN(changedValue)) {
+    // eslint-disable-next-line @typescript-eslint/no-base-to-string
     throw new Error(`Cannot convert to float: ${value.toString()}`);
   }
   return changedValue;
@@ -155,11 +157,11 @@ export const getRandomFloatRounded = (max = Number.MAX_VALUE, min = 0, scale = 2
 export const getRandomFloatFluctuatedRounded = (
   staticValue: number,
   fluctuationPercent: number,
-  scale = 2
+  scale = 2,
 ): number => {
   if (fluctuationPercent < 0 || fluctuationPercent > 100) {
     throw new RangeError(
-      `Fluctuation percent must be between 0 and 100. Actual value: ${fluctuationPercent}`
+      `Fluctuation percent must be between 0 and 100. Actual value: ${fluctuationPercent}`,
     );
   }
   if (fluctuationPercent === 0) {
@@ -169,10 +171,14 @@ export const getRandomFloatFluctuatedRounded = (
   return getRandomFloatRounded(
     staticValue * (1 + fluctuationRatio),
     staticValue * (1 - fluctuationRatio),
-    scale
+    scale,
   );
 };
 
+export const extractTimeSeriesValues = (timeSeries: Array<TimestampedData>): number[] => {
+  return timeSeries.map((timeSeriesItem) => timeSeriesItem.value);
+};
+
 export const isObject = (item: unknown): boolean => {
   return (
     isNullOrUndefined(item) === false && typeof item === 'object' && Array.isArray(item) === false
@@ -192,7 +198,7 @@ export const isCFEnvironment = (): boolean => {
 };
 
 export const isIterable = <T>(obj: T): boolean => {
-  return !isNullOrUndefined(obj) ? typeof obj[Symbol.iterator] === 'function' : false;
+  return !isNullOrUndefined(obj) ? typeof obj[Symbol.iterator as keyof T] === 'function' : false;
 };
 
 const isString = (value: unknown): boolean => {
@@ -261,7 +267,7 @@ export const promiseWithTimeout = async <T>(
   timeoutError: Error,
   timeoutCallback: () => void = () => {
     /* This is intentional */
-  }
+  },
 ): Promise<T> => {
   // Create a timeout promise that rejects in timeout milliseconds
   const timeoutPromise = new Promise<never>((_, reject) => {
@@ -289,11 +295,11 @@ export const secureRandom = (): number => {
 
 export const JSONStringifyWithMapSupport = (
   obj: Record<string, unknown> | Record<string, unknown>[] | Map<unknown, unknown>,
-  space?: number
+  space?: number,
 ): string => {
   return JSON.stringify(
     obj,
-    (key, value: Record<string, unknown>) => {
+    (_, value: Record<string, unknown>) => {
       if (value instanceof Map) {
         return {
           dataType: 'Map',
@@ -302,7 +308,7 @@ export const JSONStringifyWithMapSupport = (
       }
       return value;
     },
-    space
+    space,
   );
 };
 
@@ -326,8 +332,12 @@ export const getWebSocketCloseEventStatusString = (code: number): string => {
       return '(For applications)';
     }
   }
-  if (!isUndefined(WebSocketCloseEventStatusString[code])) {
-    return WebSocketCloseEventStatusString[code] as string;
+  if (
+    !isUndefined(
+      WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString],
+    )
+  ) {
+    return WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString];
   }
   return '(Unknown)';
 };