| |
|
|
| export type LsBoolEncoding = 'true' | '1'; |
|
|
| export function lsGet(key: string): string | null { |
| try { |
| return localStorage.getItem(key); |
| } catch { |
| return null; |
| } |
| } |
|
|
| export function lsSet(key: string, value: string): void { |
| try { |
| localStorage.setItem(key, value); |
| } catch { |
| |
| } |
| } |
|
|
| |
| export function lsSetCatch(key: string, value: string): unknown | undefined { |
| try { |
| localStorage.setItem(key, value); |
| return undefined; |
| } catch (e) { |
| return e; |
| } |
| } |
|
|
| export function lsRemove(key: string): void { |
| try { |
| localStorage.removeItem(key); |
| } catch { |
| |
| } |
| } |
|
|
| |
| |
| |
| |
| export function lsReadBool( |
| key: string, |
| defaultWhenNull: boolean, |
| options?: { encoding?: LsBoolEncoding }, |
| ): boolean { |
| const encoding = options?.encoding ?? 'true'; |
| const v = lsGet(key); |
| if (v === null) return defaultWhenNull; |
| return encoding === '1' ? v === '1' : v === 'true'; |
| } |
|
|
| export function lsWriteBool(key: string, value: boolean, encoding: LsBoolEncoding = 'true'): void { |
| lsSet(key, encoding === '1' ? (value ? '1' : '0') : value ? 'true' : 'false'); |
| } |
|
|
| export type LsReadNumberOptions = { |
| parse?: 'int' | 'float'; |
| clamp?: (n: number) => number; |
| validate?: (n: number) => boolean; |
| }; |
|
|
| export function lsReadNumber( |
| key: string, |
| defaultValue: number, |
| options?: LsReadNumberOptions, |
| ): number { |
| const v = lsGet(key); |
| if (v === null) return defaultValue; |
| const n = options?.parse === 'float' ? parseFloat(v) : parseInt(v, 10); |
| if (!Number.isFinite(n)) return defaultValue; |
| if (options?.validate && !options.validate(n)) return defaultValue; |
| return options?.clamp ? options.clamp(n) : n; |
| } |
|
|
| export function lsWriteNumber(key: string, value: number): void { |
| lsSet(key, String(value)); |
| } |
|
|
| export function lsReadEnum<T extends string>( |
| key: string, |
| allowed: readonly T[], |
| defaultValue: T, |
| ): T { |
| const v = lsGet(key); |
| if (v !== null && (allowed as readonly string[]).includes(v)) return v as T; |
| return defaultValue; |
| } |
|
|
| export function lsWriteString(key: string, value: string): void { |
| lsSet(key, value); |
| } |
|
|