Skip to content

Commit

Permalink
add localStorage functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkIntaqt committed Feb 15, 2023
1 parent 4282b1f commit 6ae88f3
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions utils/localStorageFunctions.js
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();
}

1 comment on commit 6ae88f3

@vercel
Copy link

@vercel vercel bot commented on 6ae88f3 Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.