MCPcopy Create free account
hub / github.com/algolia/docsearch / createStorage

Function createStorage

packages/docsearch-react/src/utils/storage.ts:124–151  ·  view source on GitHub ↗
(key: string)

Source from the content-addressed store, hash-verified

122 */
123// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
124export 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.

Callers 3

createStoredSearchesFunction · 0.90
utils.test.tsFile · 0.90

Calls 1

isLocalStorageSupportedFunction · 0.85

Tested by

no test coverage detected