Small cleanups in Utils.ts
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index d3b9872da8206bb0f840ccc975bc137302e41e91..f27f06660227643ebe516f8972ee08206334af22 100644 (file)
@@ -50,45 +50,42 @@ export default class Utils {
     return Utils.formatDurationMilliSeconds(duration * 1000);
   }
 
-  public static convertToDate(value: unknown): Date {
-    // Check
-    if (!value) {
-      return value as Date;
+  public static convertToDate(value: unknown): Date | null | undefined {
+    if (Utils.isNullOrUndefined(value)) {
+      return value as null | undefined;
+    }
+    if (value instanceof Date) {
+      return value;
     }
-    // Check Type
-    if (!(value instanceof Date)) {
-      return new Date(value as string);
+    if (Utils.isString(value) || typeof value === 'number') {
+      return new Date(value as string | number);
     }
-    return value;
+    return null;
   }
 
   public static convertToInt(value: unknown): number {
-    let changedValue: number = value as number;
     if (!value) {
       return 0;
     }
+    let changedValue: number = value as number;
     if (Number.isSafeInteger(value)) {
       return value as number;
     }
-    // Check
-    if (Utils.isString(value)) {
-      // Create Object
-      changedValue = parseInt(value as string);
-    }
     if (typeof value === 'number') {
       changedValue = Math.trunc(value);
     }
+    if (Utils.isString(value)) {
+      changedValue = parseInt(value as string);
+    }
     return changedValue;
   }
 
   public static convertToFloat(value: unknown): number {
-    let changedValue: number = value as number;
     if (!value) {
       return 0;
     }
-    // Check
+    let changedValue: number = value as number;
     if (Utils.isString(value)) {
-      // Create Object
       changedValue = parseFloat(value as string);
     }
     return changedValue;