Created
June 15, 2013 11:38
-
-
Save mctep/5787849 to your computer and use it in GitHub Desktop.
JSON Strorage
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
var jsonStorage = { | |
get: function(key) { | |
try { | |
return JSON.parse(localStorage.getItem(key)) || null; | |
} catch(e) { | |
return null; | |
} | |
}, | |
set: function(key, value) { | |
if (key && undefined !== value) { | |
localStorage.setItem(key, JSON.stringify(value)); | |
} else { | |
throw new Error('Error: `key` and/or `value` is undefined'); | |
} | |
}, | |
remove: function(key) { | |
if (key) { | |
localStorage.removeItem(key); | |
} else { | |
throw new Error('Error: `key` and/or `value` is undefined'); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment