Skip to content

Instantly share code, notes, and snippets.

@AliBahaari
Created January 12, 2023 19:04
Show Gist options
  • Save AliBahaari/1c2ff059fc2e58c2a90d44af8b2514ad to your computer and use it in GitHub Desktop.
Save AliBahaari/1c2ff059fc2e58c2a90d44af8b2514ad to your computer and use it in GitHub Desktop.
A function for handling local storage manipulations!
const localStorageController = <T extends string = "", K = "">(
storageName: T,
type: "GET" | "SET" | "DELETE",
storageValue?: K
) => {
if (global?.window !== undefined && window !== undefined) {
if (type === "GET") {
const fetchedValue = window.localStorage.getItem(storageName);
if (fetchedValue) {
return JSON.parse(fetchedValue);
}
} else if (type === "SET") {
window.localStorage.setItem(storageName, JSON.stringify(storageValue));
} else if (type === "DELETE") {
window.localStorage.removeItem(storageName);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment