Created
January 12, 2023 19:04
-
-
Save AliBahaari/1c2ff059fc2e58c2a90d44af8b2514ad to your computer and use it in GitHub Desktop.
A function for handling local storage manipulations!
This file contains hidden or 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
| 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