refactor(ui): validate UUID format
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / Utils.ts
index 92f687d85302cf0cd56949fc800b899390459f47..0c8a83996e905c7e046d624ad9a8bd64124d1df1 100644 (file)
@@ -1,47 +1,67 @@
-export default class Utils {
-  // STATE
-  public static isUndefined(value: unknown): boolean {
-    return typeof value === 'undefined';
-  }
+import { UIClient } from './UIClient'
 
-  public static ifUndefined<T>(value: T | undefined, isValue: T): T {
-    if (Utils.isUndefined(value) === true) return isValue;
-    return value as T;
+export const convertToBoolean = (value: unknown): boolean => {
+  let result = false
+  if (value != null) {
+    // Check the type
+    if (typeof value === 'boolean') {
+      return value
+    } else if (typeof value === 'string' && (value.toLowerCase() === 'true' || value === '1')) {
+      result = true
+    } else if (typeof value === 'number' && value === 1) {
+      result = true
+    }
   }
+  return result
+}
 
-  public static isIterable<T extends Iterable<T>>(obj: T): boolean {
-    if (obj === null || obj === undefined) {
-      return false;
-    }
-    return typeof obj[Symbol.iterator] === 'function';
+export const convertToInt = (value: unknown): number => {
+  if (value == null) {
+    return 0
+  }
+  if (Number.isSafeInteger(value)) {
+    return value as number
+  }
+  if (typeof value === 'number') {
+    return Math.trunc(value)
   }
+  let changedValue: number = value as number
+  if (typeof value === 'string') {
+    changedValue = parseInt(value)
+  }
+  if (isNaN(changedValue)) {
+    throw new Error(`Cannot convert to integer: '${String(value)}'`)
+  }
+  return changedValue
+}
 
-  // public static ifNotIterableDo<T>(obj: T, cb: () => void): void {
-  //   if (this.isIterable(obj) === false) cb();
-  // }
+export const getFromLocalStorage = <T>(key: string, defaultValue: T): T => {
+  const item = localStorage.getItem(key)
+  return item != null ? (JSON.parse(item) as T) : defaultValue
+}
 
-  public static async promiseWithTimeout<T>(
-    promise: Promise<T>,
-    timeoutMs: number,
-    timeoutError: Error,
-    timeoutCallback: () => void = () => {
-      /* This is intentional */
-    }
-  ): Promise<T> {
-    // Create a timeout promise that rejects in timeout milliseconds
-    const timeoutPromise = new Promise<never>((_, reject) => {
-      setTimeout(() => {
-        timeoutCallback();
-        reject(timeoutError);
-      }, timeoutMs);
-    });
-
-    // Returns a race between timeout promise and the passed promise
-    return Promise.race<T>([promise, timeoutPromise]);
-  }
+export const setToLocalStorage = <T>(key: string, value: T): void => {
+  localStorage.setItem(key, JSON.stringify(value))
+}
+
+export const deleteFromLocalStorage = (key: string): void => {
+  localStorage.removeItem(key)
+}
+
+export const getLocalStorage = (): Storage => {
+  return localStorage
+}
+
+export const randomUUID = (): `${string}-${string}-${string}-${string}-${string}` => {
+  return crypto.randomUUID()
+}
+
+export const validateUUID = (
+  uuid: `${string}-${string}-${string}-${string}-${string}`
+): uuid is `${string}-${string}-${string}-${string}-${string}` => {
+  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)
+}
 
-  // FUNCTIONAL
-  // public static compose<T>(...fns: ((arg: T) => T)[]): (x: T) => T {
-  //   return (x: T) => fns.reduceRight((y, fn) => fn(y), x);
-  // }
+export const useUIClient = (): UIClient => {
+  return UIClient.getInstance()
 }