X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=94fa3d186e432712238ceee87e7f3491244cb5b7;hb=1f7a03460c25a4d696d23a1a4ff6fbe7384346e9;hp=08e87320ce2c83f9ab563c46e53ec01718723cb1;hpb=c60af6ca67ad2599e7e3e886f219da6323ead61b;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 08e87320..94fa3d18 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -52,7 +52,9 @@ export default class Utils { return Utils.formatDurationMilliSeconds(duration * 1000); } - public static convertToDate(value: unknown): Date | null | undefined { + public static convertToDate( + value: Date | string | number | null | undefined + ): Date | null | undefined { if (Utils.isNullOrUndefined(value)) { return value as null | undefined; } @@ -60,7 +62,7 @@ export default class Utils { return value; } if (Utils.isString(value) || typeof value === 'number') { - return new Date(value as string | number); + return new Date(value); } return null; } @@ -74,11 +76,14 @@ export default class Utils { return value as number; } if (typeof value === 'number') { - changedValue = Math.trunc(value); + return Math.trunc(value); } if (Utils.isString(value)) { changedValue = parseInt(value as string); } + if (isNaN(changedValue)) { + throw new Error(`Cannot convert to integer: ${value.toString()}`); + } return changedValue; } @@ -90,6 +95,9 @@ export default class Utils { if (Utils.isString(value)) { changedValue = parseFloat(value as string); } + if (isNaN(changedValue)) { + throw new Error(`Cannot convert to float: ${value.toString()}`); + } return changedValue; } @@ -98,7 +106,7 @@ export default class Utils { if (value) { // Check the type if (typeof value === 'boolean') { - result = value; + return value; } else if ( Utils.isString(value) && ((value as string).toLowerCase() === 'true' || value === '1') @@ -220,7 +228,7 @@ export default class Utils { `${str.slice(0, pos)}${subStr}${str.slice(pos)}`; /** - * @param [retryNumber=0] + * @param retryNumber - the number of retries that have already been attempted * @returns delay in milliseconds */ public static exponentialDelay(retryNumber = 0): number { @@ -280,7 +288,7 @@ export default class Utils { /** * Convert websocket error code to human readable string message * - * @param code websocket error code + * @param code - websocket error code * @returns human readable string message */ public static getWebSocketCloseEventStatusString(code: number): string {