X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fcomposables%2FUtils.ts;h=fffd7a54966a54a2e092d4eef2a4d5360b45036b;hb=0344ad2b24f8b043b5848a0fea8b5a120d6781db;hp=9e1277a84beaeea04d35c0ec4bcc6fd749a8b79c;hpb=6ebeab4b8404e5a9d69b3dcdff47b9087b2058f0;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 9e1277a8..fffd7a54 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,45 +1,43 @@ -const 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 +} -export const ifUndefined = (value: T | undefined, isValue: T): T => { - if (isUndefined(value) === true) return isValue; - return value as T; -}; +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 +} -// const isIterable = (obj: T): boolean => { -// if (obj === null || obj === undefined) { -// return false; -// } -// return typeof (obj as unknown as Iterable)[Symbol.iterator] === 'function'; -// }; +export const setToLocalStorage = (key: string, value: T): void => { + localStorage.setItem(key, JSON.stringify(value)) +} -// const ifNotIterableDo = (obj: T, cb: () => void): void => { -// if (isIterable(obj) === false) cb(); -// }; - -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(() => { - // FIXME: The original promise state shall be checked - 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); -// }; +export const getFromLocalStorage = (key: string, defaultValue: T): T => { + const item = localStorage.getItem(key) + return item != null ? (JSON.parse(item) as T) : defaultValue +}