X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=0c8a83996e905c7e046d624ad9a8bd64124d1df1;hb=5218eec27c72bade7112c6b029d7d1eb1f42f871;hp=8cb957abba2c2e10a0edfc20d7daeb29b0a0b69f;hpb=2eb6954d7bc5bb0e88a3b20aa62db7b8b08395d1;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 8cb957ab..0c8a8399 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,56 +1,67 @@ -import util from 'node:util'; +import { UIClient } from './UIClient' -export default class Utils { - // STATE - public static isUndefined(value: unknown): boolean { - return typeof value === 'undefined'; +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 ifUndefined(value: T | undefined, isValue: T): T { - if (Utils.isUndefined(value) === true) return isValue; - return value as T; +export const convertToInt = (value: unknown): number => { + if (value == null) { + return 0 } - - public static isIterable(obj: T): boolean { - if (obj === null || obj === undefined) { - return false; - } - return typeof (obj as any)[Symbol.iterator] === 'function'; + 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(obj: T, cb: () => void): void { - // if (this.isIterable(obj) === false) cb(); - // } +export const getFromLocalStorage = (key: string, defaultValue: T): T => { + const item = localStorage.getItem(key) + return item != null ? (JSON.parse(item) as T) : defaultValue +} - public static isPromisePending(promise: Promise): boolean { - return util.inspect(promise).includes('pending'); - } +export const setToLocalStorage = (key: string, value: T): void => { + localStorage.setItem(key, JSON.stringify(value)) +} - public static async promiseWithTimeout( - promise: Promise, - timeoutMs: number, - timeoutError: Error, - timeoutCallback: () => void = () => { - /* This is intentional */ - } - ): Promise { - // Create a timeout promise that rejects in timeout milliseconds - const timeoutPromise = new Promise((_, reject) => { - setTimeout(() => { - if (Utils.isPromisePending(promise)) { - timeoutCallback(); - // FIXME: The original promise shall be canceled - } - reject(timeoutError); - }, timeoutMs); - }); - - // Returns a race between timeout promise and the passed promise - return Promise.race([promise, timeoutPromise]); - } +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(...fns: ((arg: T) => T)[]): (x: T) => T { - // return (x: T) => fns.reduceRight((y, fn) => fn(y), x); - // } +export const useUIClient = (): UIClient => { + return UIClient.getInstance() }