Created
October 2, 2019 14:24
-
-
Save lski/cd5b061a5de46ac2e644d8e182bbd031 to your computer and use it in GitHub Desktop.
A simple localStorage wrapper storing objects, rather than strings
This file contains 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
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