Skip to content

Instantly share code, notes, and snippets.

@mctep
Created June 15, 2013 11:38
Show Gist options
  • Save mctep/5787849 to your computer and use it in GitHub Desktop.
Save mctep/5787849 to your computer and use it in GitHub Desktop.
JSON Strorage
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