Created
January 30, 2019 10:27
-
-
Save yairEO/d4eaa560033d23284f1a2c2308818ff0 to your computer and use it in GitHub Desktop.
localstorge helper
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
const localstore = { | |
NAMESPACE : '', | |
/** | |
* Set localStorage value in a given key | |
* @param {String} name | |
* @param {*} value | |
*/ | |
set( name, value='' ){ | |
try { | |
return window.localStorage[localstore.NAMESPACE + name] = JSON.stringify(value); | |
} | |
catch(err){ | |
return false; | |
} | |
}, | |
/** | |
* Get a value from the localStorage by a given key | |
* @param {string} name | |
*/ | |
get( name ){ | |
var value = window.localStorage[localstore.NAMESPACE + name]; | |
try{ | |
//console.warn(name + " - parsed JSON: ", value); | |
return JSON.parse(value); | |
} | |
catch(err){ | |
//console.warn(name + " - couldn't parse JSON: ", value); | |
return value; | |
} | |
}, | |
remove( name ){ | |
window.localStorage.removeItem(localstore.NAMESPACE + name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment