9300148668c260790a8dd8b4e1f71fe136884112
1 import { UIClient
} from
'./UIClient'
3 export const convertToBoolean
= (value
: unknown
): boolean => {
7 if (typeof value
=== 'boolean') {
9 } else if (typeof value
=== 'string' && (value
.toLowerCase() === 'true' || value
=== '1')) {
11 } else if (typeof value
=== 'number' && value
=== 1) {
18 export const convertToInt
= (value
: unknown
): number => {
22 if (Number.isSafeInteger(value
)) {
23 return value
as number
25 if (typeof value
=== 'number') {
26 return Math.trunc(value
)
28 let changedValue
: number = value
as number
29 if (typeof value
=== 'string') {
30 changedValue
= Number.parseInt(value
)
32 if (isNaN(changedValue
)) {
33 throw new Error(`Cannot convert to integer: '${String(value)}'`)
38 export const getFromLocalStorage
= <T
>(key
: string, defaultValue
: T
): T
=> {
39 const item
= localStorage
.getItem(key
)
40 return item
!= null ? (JSON
.parse(item
) as T
) : defaultValue
43 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
44 export const setToLocalStorage
= <T
>(key
: string, value
: T
): void => {
45 localStorage
.setItem(key
, JSON
.stringify(value
))
48 export const deleteFromLocalStorage
= (key
: string): void => {
49 localStorage
.removeItem(key
)
52 export const getLocalStorage
= (): Storage
=> {
56 export const randomUUID
= (): `${string}-${string}-${string}-${string}-${string}` => {
57 return crypto
.randomUUID()
60 export const validateUUID
= (
61 uuid
: `${string}-${string}-${string}-${string}-${string}`
62 ): uuid
is `${string}-${string}-${string}-${string}-${string}` => {
63 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
)
66 export const useUIClient
= (): UIClient
=> {
67 return UIClient
.getInstance()