refactor(ui): validate UUID format
[e-mobility-charging-stations-simulator.git] / ui / web / src / composables / Utils.ts
index 451c4155fd7a114990f28f52fb619fc4932d774d..0c8a83996e905c7e046d624ad9a8bd64124d1df1 100644 (file)
@@ -1,3 +1,5 @@
+import { UIClient } from './UIClient'
+
 export const convertToBoolean = (value: unknown): boolean => {
   let result = false
   if (value != null) {
@@ -32,3 +34,34 @@ export const convertToInt = (value: unknown): number => {
   }
   return changedValue
 }
+
+export const getFromLocalStorage = <T>(key: string, defaultValue: T): T => {
+  const item = localStorage.getItem(key)
+  return item != null ? (JSON.parse(item) as T) : defaultValue
+}
+
+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)
+}
+
+export const useUIClient = (): UIClient => {
+  return UIClient.getInstance()
+}