X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=8d51349a2357fbb9ea0b450dd3367f00f8c7406e;hb=a37fc6dc8267e22b2b2d35773525980b81f014e8;hp=f15224a855b7e08e53e75e4221bb1009a80f19cd;hpb=9bf0ef23c51160abc6866ad8d07eea85e308edb8;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index f15224a8..8d51349a 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -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): 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 = (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 ( timeoutError: Error, timeoutCallback: () => void = () => { /* This is intentional */ - } + }, ): Promise => { // Create a timeout promise that rejects in timeout milliseconds const timeoutPromise = new Promise((_, reject) => { @@ -289,11 +295,11 @@ export const secureRandom = (): number => { export const JSONStringifyWithMapSupport = ( obj: Record | Record[] | Map, - space?: number + space?: number, ): string => { return JSON.stringify( obj, - (key, value: Record) => { + (_, value: Record) => { 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)'; };