Last active
December 22, 2023 11:12
-
-
Save PabloAballe/63ba3c555f3335276f66129a58dc16ad to your computer and use it in GitHub Desktop.
LocalStorage Helper Function: Simplifies the use of LocalStorage in web applications.
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
| /** | |
| * LocalStorage Helper Function | |
| * Brief Description: Simplifies the use of LocalStorage in web applications. | |
| * | |
| * Author: Pablo Aballe | |
| * Date: 2023-12-22 | |
| */ | |
| /** | |
| * Simplifies setting, getting, and removing items in LocalStorage. | |
| */ | |
| const localStorageHelper = { | |
| setItem(key, value) { | |
| localStorage.setItem(key, JSON.stringify(value)); | |
| }, | |
| getItem(key) { | |
| const item = localStorage.getItem(key); | |
| return item ? JSON.parse(item) : null; | |
| }, | |
| removeItem(key) { | |
| localStorage.removeItem(key); | |
| } | |
| }; | |
| // Usage Example | |
| localStorageHelper.setItem('user', { name: 'John Doe', age: 30 }); | |
| console.log(localStorageHelper.getItem('user')); // { name: 'John Doe', age: 30 } | |
| localStorageHelper.removeItem('user'); | |
| // Additional Notes: | |
| // This helper abstracts the JSON parsing/stringifying. Always check for null and exceptions in real-world applications. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment