I wasn't quite happy with the way localStorage handles non-string values, for example localStorage.setItem("foo",{test:5});localStorage.getItem("foo");
returns string literal '[object Object]'
, so i thought it would be a good idea to wrap it in json, came up with
betterLocalStorage = {
get: function(key) {
return JSON.parse(localStorage.getItem(key));
},
set: function(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
};
and betterLocalStorage.set("foo",{test:5});betterLocalStorage.get("foo");
returns object {test: 5}