-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4282b1f
commit 6ae88f3
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Keys used for localStorage. | ||
*/ | ||
export const storageKeys = { | ||
challengeFilters: "challenge-filters", | ||
challengeSearch: "challenge-search" | ||
}; | ||
|
||
/** | ||
* Get the JSON parsed value by key from localStorage. If no value is found, | ||
* a default value can optionally be used as the return. | ||
* @param {string} key localStorage key | ||
* @param {any} defaultValue default value to return if no value was found | ||
* @returns {any} | ||
*/ | ||
export function getStorage(key, defaultValue) { | ||
let value = JSON.parse(localStorage.getItem(key)); | ||
if (value == null) value = defaultValue ?? undefined; | ||
return value; | ||
} | ||
|
||
/** | ||
* Sets the JSON stringified value of key into localStorage. | ||
* @param {string} key | ||
* @param {any} value | ||
*/ | ||
export function setStorage(key, value) { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
} | ||
|
||
/** | ||
* Removes the JSON value associated with key from localStorage. | ||
* @param {string} key | ||
*/ | ||
export function removeStorage(key) { | ||
localStorage.removeItem(key); | ||
} | ||
|
||
/** | ||
* Clears all values from localStorage. Be careful using this. ❤️ | ||
*/ | ||
export function clearStorage() { | ||
localStorage.clear(); | ||
} |
6ae88f3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
challenges – ./
challenges-git-next-darkintaqt.vercel.app
challenges-darkintaqt.vercel.app
challenges-beta.darkintaqt.com