Skip to content

Instantly share code, notes, and snippets.

@Jxck
Created August 21, 2010 15:18
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 Jxck/542437 to your computer and use it in GitHub Desktop.
Save Jxck/542437 to your computer and use it in GitHub Desktop.
var db = {
set: function(key, obj) {
localStorage.setItem(key, JSON.stringify(obj));
},
get: function(key) {
var tmp = localStorage.getItem(key);
return (tmp === undefined) ? null : JSON.parse(tmp);
},
each: function(fun) {
try {
for (var i = 0; i < localStorage.length; i++) {
var k = localStorage.key(i);
fun(k, db.get(k));
}
}catch (e) {
for (var key in localStorage) {
if (key === 'key') continue;
fun(k, db.get(k));
}
}
},
del: function(key) {
delete localStorage[key];
},
clear: function() {
localStorage.clear();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment