Skip to content

Commit

Permalink
feat: local storage helper
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Oct 30, 2023
1 parent c563b01 commit deb0b67
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions electron/renderer/lib/local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const hasLocalStorage = (): boolean => {
return typeof localStorage !== 'undefined';
};

export const LocalStorage = {
/**
* Gets a JSON value from local storage for the given key.
* If either local storage isn't defined or key not found then
* returns undefined.
*/
get: <T>(key: string): T | undefined => {
if (!hasLocalStorage()) {
return;
}
const value = localStorage.getItem(key);
if (value) {
return JSON.parse(value);
}
return;
},
/**
* Sets a JSON value in local storage for the given key.
* The value should be JSON serializable.
* For example, not a `Map` or `Set` because those become `{}`.
*/
set: <T>(key: string, value: T): void => {
if (!hasLocalStorage()) {
return;
}
localStorage.setItem(key, JSON.stringify(value));
},
};

0 comments on commit deb0b67

Please sign in to comment.