X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=b1bebc92582b5ae64c82b6839ab244a05c0c6075;hb=f2a90135692aac9beaeeb73efcb7e7aa093ec39a;hp=ea51bc23121a2b47d4813a5098843564cae31c91;hpb=a807045be19c1ed4996a44d8c2c8774e926dc6dc;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index ea51bc23..b1bebc92 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,23 +1,67 @@ -const isUndefined = (value: unknown): boolean => { - return typeof value === 'undefined' +import { UIClient } from './UIClient' + +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 +} + +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 = Number.parseInt(value) + } + if (isNaN(changedValue)) { + throw new Error(`Cannot convert to integer: '${String(value)}'`) + } + 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 deleteFromLocalStorage = (key: string): void => { + localStorage.removeItem(key) } -export const ifUndefined = (value: T | undefined, isValue: T): T => { - if (isUndefined(value) === true) return isValue - return value as T +export const getLocalStorage = (): Storage => { + return localStorage } -// const isIterable = (obj: T): boolean => { -// if (obj == null) { -// return false -// } -// return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function' -// } +export const randomUUID = (): `${string}-${string}-${string}-${string}-${string}` => { + return crypto.randomUUID() +} -// const ifNotIterableDo = (obj: T, cb: () => void): void => { -// if (isIterable(obj) === false) cb() -// } +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 compose = (...fns: ((arg: T) => T)[]): ((x: T) => T) => { -// return (x: T) => fns.reduceRight((y, fn) => fn(y), x) -// } +export const useUIClient = (): UIClient => { + return UIClient.getInstance() +}