From: Jérôme Benoit Date: Thu, 22 Feb 2024 20:36:29 +0000 (+0100) Subject: build(ui): add missing file X-Git-Tag: v1.2.38~58 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4940561f080511ee4178df364222765d65289ffe;p=e-mobility-charging-stations-simulator.git build(ui): add missing file Signed-off-by: Jérôme Benoit --- diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts new file mode 100644 index 00000000..5638efe2 --- /dev/null +++ b/ui/web/src/composables/Utils.ts @@ -0,0 +1,34 @@ +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 + } + let changedValue: number = value as number + if (Number.isSafeInteger(value)) { + return value as number + } + if (typeof value === 'number') { + return Math.trunc(value) + } + if (typeof value === 'string') { + changedValue = parseInt(value) + } + if (isNaN(changedValue)) { + throw new Error(`Cannot convert to integer: '${String(value)}'`) + } + return changedValue +}