Skip to content

Instantly share code, notes, and snippets.

@lski
Created October 2, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lski/cd5b061a5de46ac2e644d8e182bbd031 to your computer and use it in GitHub Desktop.
Save lski/cd5b061a5de46ac2e644d8e182bbd031 to your computer and use it in GitHub Desktop.
A simple localStorage wrapper storing objects, rather than strings
function setLocationStorage(key, value) {
localStorage.setItem(key, JSON.stringify(value));
return value;
}
function getLocationStorage(key, clear = false) {
const value = localStorage.getItem(key);
if(clear === true) {
removeLocationStorage(key);
}
if(value === (void 0) || value === null) {
return value;
}
return JSON.parse(value);
}
function removeLocationStorage(key) {
localStorage.removeItem(key);
}
export {
setLocationStorage,
getLocationStorage,
removeLocationStorage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment