json-storage.js/lib/index.js

80 lines
1.4 KiB
JavaScript
Raw Normal View History

(function () {
"use strict";
var db;
function Stringify(obj) {
var str;
try {
str = JSON.stringify(obj);
} catch(e) {
str = "";
}
return str;
}
function Parse(str) {
var obj = null;
try {
obj = JSON.parse(str);
} catch(e) {}
return obj;
}
function JsonStorage(w3cStorage) {
this.db = w3cStorage;
this.keysAreDirty = true;
}
db = JsonStorage;
db.prototype.clear = function () {
this.keysAreDirty = true;
2011-09-05 21:40:52 +00:00
this.keys().forEach(function (uuid) {
this.remove(uuid);
}, this);
};
db.prototype.remove = function (uuid) {
this.keysAreDirty = true;
this.db.removeItem(uuid);
};
db.prototype.get = function (uuid) {
return Parse(this.db.getItem(uuid));
};
db.prototype.set = function (uuid, val) {
this.keysAreDirty = true;
return this.db.setItem(uuid, Stringify(val));
};
db.prototype.keys = function () {
2011-09-05 21:40:52 +00:00
var i
;
if (!this.keysAreDirty) {
2011-09-05 21:40:52 +00:00
return this.__keys.concat([]);
}
2011-09-05 21:40:52 +00:00
this.__keys = [];
for (i = 0; i < this.db.length; i += 1) {
2011-09-05 21:40:52 +00:00
this.__keys.push(this.db.key(i));
}
this.keysAreDirty = false;
2011-09-05 21:40:52 +00:00
return this.__keys.concat([]);
};
db.prototype.size = function () {
return this.db.length;
};
function create(w3cStorage) {
return new JsonStorage(w3cStorage);
}
module.exports = create;
}());