X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=b1bebc92582b5ae64c82b6839ab244a05c0c6075;hb=f2a90135692aac9beaeeb73efcb7e7aa093ec39a;hp=fffd7a54966a54a2e092d4eef2a4d5360b45036b;hpb=0344ad2b24f8b043b5848a0fea8b5a120d6781db;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index fffd7a54..b1bebc92 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,3 +1,5 @@ +import { UIClient } from './UIClient' + export const convertToBoolean = (value: unknown): boolean => { let result = false if (value != null) { @@ -25,7 +27,7 @@ export const convertToInt = (value: unknown): number => { } let changedValue: number = value as number if (typeof value === 'string') { - changedValue = parseInt(value) + changedValue = Number.parseInt(value) } if (isNaN(changedValue)) { throw new Error(`Cannot convert to integer: '${String(value)}'`) @@ -33,11 +35,33 @@ export const convertToInt = (value: unknown): number => { return changedValue } +export const getFromLocalStorage = (key: string, defaultValue: T): T => { + const item = localStorage.getItem(key) + return item != null ? (JSON.parse(item) as T) : defaultValue +} + export const setToLocalStorage = (key: string, value: T): void => { localStorage.setItem(key, JSON.stringify(value)) } -export const getFromLocalStorage = (key: string, defaultValue: T): T => { - const item = localStorage.getItem(key) - return item != null ? (JSON.parse(item) as T) : defaultValue +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() }