X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=0c8a83996e905c7e046d624ad9a8bd64124d1df1;hb=5218eec27c72bade7112c6b029d7d1eb1f42f871;hp=93499abce4e42398b1f7d499281494da136d3684;hpb=f3095697dad156dcb13ea8247a16eb08ca007e6b;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 93499abc..0c8a8399 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,52 +1,67 @@ -import util from 'node:util'; - -const isUndefined = (value: unknown): boolean => { - return typeof value === 'undefined'; -}; - -export const ifUndefined = (value: T | undefined, isValue: T): T => { - if (isUndefined(value) === true) return isValue; - return value as T; -}; - -// const isIterable = (obj: T): boolean => { -// if (obj === null || obj === undefined) { -// return false; -// } -// return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function'; -// }; - -// const ifNotIterableDo = (obj: T, cb: () => void): void => { -// if (isIterable(obj) === false) cb(); -// }; - -const isPromisePending = (promise: Promise): boolean => { - return util.inspect(promise).includes('pending'); -}; - -export const 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 (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 compose = (...fns: ((arg: T) => T)[]): ((x: T) => T) => { -// return (x: T) => fns.reduceRight((y, fn) => fn(y), x); -// }; +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 = 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 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() +}