json-storage.js/tests/node_modules/json-storage/json-storage.js

78 lines
1.3 KiB
JavaScript

(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;
self.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 () {
var i;
if (!this.keysAreDirty) {
return this.keys;
}
this.keys = [];
for (i = 0; i < this.db.length; i += 1) {
this.keys.push(this.db.key(i));
}
this.keysAreDirty = false;
return this.keys.concat([]);
};
db.prototype.size = function () {
return this.db.length;
};
function create(w3cStorage) {
return new JsonStorage(w3cStorage);
}
module.exports = create;
}());