diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-10-26 19:07:31 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-11 18:15:27 +0100 |
| commit | 795b92cc1a168c48c7710ca6e0e1ef5974013d95 (patch) | |
| tree | 8f57204b0ffe7c8acb3203a24292f375377b6369 /src/services/local-storage/index.ts | |
| parent | 9aeb82269d7c74c4566b7ca254782a4dfbd69a6e (diff) | |
refactor(hooks): rewrite useLocalStorage hook
* return a tuple instead of an object
* add a validator function as parameter (if the stored value
is manually changed, it is not safe to cast its type)
* add tests
Diffstat (limited to 'src/services/local-storage/index.ts')
| -rw-r--r-- | src/services/local-storage/index.ts | 27 |
1 files changed, 1 insertions, 26 deletions
diff --git a/src/services/local-storage/index.ts b/src/services/local-storage/index.ts index 65235a7..72dd18f 100644 --- a/src/services/local-storage/index.ts +++ b/src/services/local-storage/index.ts @@ -1,26 +1 @@ -export const LocalStorage = { - get<T>(key: string): T | undefined { - try { - const serialItem = localStorage.getItem(key); - if (!serialItem) return undefined; - return JSON.parse(serialItem) as T; - } catch (e) { - console.log(e); - return undefined; - } - }, - set<T>(key: string, value: T) { - try { - const serialItem = JSON.stringify(value); - localStorage.setItem(key, serialItem); - } catch (e) { - console.log(e); - } - }, - remove(key: string) { - localStorage.removeItem(key); - }, - clear() { - localStorage.clear(); - }, -}; +export * from './local-storage'; |
