localstorage.js: bug fix

If you stored "false" and tried to retrieve it with a default value, you got
the default value instead of "false".
This commit is contained in:
hallgren
2014-04-09 20:49:53 +00:00
parent 96f36d8f0a
commit 402cccc3ca

View File

@@ -15,15 +15,15 @@ var fakedLocalStorage = [] // a shared substitute for persistent localStorage
// An interface to localStorage, to store JSON data under a unique prefix
function appLocalStorage(appPrefix,privateStorage) {
function parse(s) {
try { return JSON.parse(s) } catch(e) { return null }
function parse(s,def) {
try { return JSON.parse(s) } catch(e) { return def }
}
function methods(storage) {
return {
get: function (name,def) {
var id=appPrefix+name
return storage[id] && parse(storage[id]) || def;
return parse(storage[id]||"",def);
},
put: function (name,value) {
var id=appPrefix+name;
@@ -34,7 +34,7 @@ function appLocalStorage(appPrefix,privateStorage) {
delete storage[id]
},
ls: function(prefix) {
var pre=appPrefix+prefix
var pre=appPrefix+(prefix||"")
var files=[]
for(var i in storage)
if(hasPrefix(i,pre)) files.push(i.substr(pre.length))