| 21 | * const hasTheme = readOnlyStore.has('theme') |
| 22 | */ |
| 23 | export class ReadOnlyValuesStore { |
| 24 | /** |
| 25 | * Underlying store values containing session data |
| 26 | */ |
| 27 | protected values: SessionData |
| 28 | |
| 29 | /** |
| 30 | * Returns true if the store is empty |
| 31 | */ |
| 32 | get isEmpty() { |
| 33 | return !this.values || Object.keys(this.values).length === 0 |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Creates a new readonly values store |
| 38 | * |
| 39 | * @param values - Initial session data or null |
| 40 | */ |
| 41 | constructor(values: SessionData | null) { |
| 42 | this.values = values || {} |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Gets value for a given key |
| 47 | * |
| 48 | * @param key - The key or key path to retrieve |
| 49 | * @param defaultValue - Default value if key doesn't exist |
| 50 | * |
| 51 | * @example |
| 52 | * store.get('username', 'guest') |
| 53 | * store.get(['user', 'preferences', 'theme'], 'light') |
| 54 | */ |
| 55 | get(key: string | string[], defaultValue?: any): any { |
| 56 | const value = lodash.get(this.values, key) |
| 57 | if (defaultValue !== undefined && (value === null || value === undefined)) { |
| 58 | return defaultValue |
| 59 | } |
| 60 | |
| 61 | return value |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Checks if a value exists. Includes extra guards to check arrays for length. |
| 66 | * |
| 67 | * @param key - The key or key path to check |
| 68 | * @param checkForArraysLength - Whether to check array length (default: true) |
| 69 | * |
| 70 | * @example |
| 71 | * store.has('username') // Check if key exists |
| 72 | * store.has(['user', 'roles']) // Check nested key |
| 73 | * store.has('items', false) // Don't check array length |
| 74 | */ |
| 75 | has(key: string | string[], checkForArraysLength: boolean = true): boolean { |
| 76 | const value = this.get(key) |
| 77 | if (!Array.isArray(value)) { |
| 78 | return !!value |
| 79 | } |
| 80 |
nothing calls this directly
no outgoing calls
no test coverage detected