Skip to content

Instantly share code, notes, and snippets.

@PabloAballe
Last active December 22, 2023 11:12
Show Gist options
  • Select an option

  • Save PabloAballe/63ba3c555f3335276f66129a58dc16ad to your computer and use it in GitHub Desktop.

Select an option

Save PabloAballe/63ba3c555f3335276f66129a58dc16ad to your computer and use it in GitHub Desktop.
LocalStorage Helper Function: Simplifies the use of LocalStorage in web applications.
/**
* 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