(key: string)
| 122 | */ |
| 123 | // eslint-disable-next-line @typescript-eslint/explicit-function-return-type |
| 124 | export function createStorage<TItem>(key: string) { |
| 125 | if (isLocalStorageSupported() === false) { |
| 126 | return { |
| 127 | setItem(): void {}, |
| 128 | getItem(): TItem[] { |
| 129 | return []; |
| 130 | }, |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | return { |
| 135 | setItem(item: TItem[]): void { |
| 136 | safeSetLocalStorageItem(key, item); |
| 137 | }, |
| 138 | getItem(): TItem[] { |
| 139 | const item = window.localStorage.getItem(key); |
| 140 | if (item === null) return []; |
| 141 | try { |
| 142 | const parsed = JSON.parse(item); |
| 143 | return Array.isArray(parsed) ? (parsed as TItem[]) : []; |
| 144 | } catch { |
| 145 | // clear corrupted data and return empty list |
| 146 | window.localStorage.removeItem(key); |
| 147 | return []; |
| 148 | } |
| 149 | }, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Creates a simple storage interface for a single object using localstorage. |
no test coverage detected