2012-08-16 19:18:48 +00:00
|
|
|
// http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
// this varies from actual localStorage in some subtle ways
|
|
|
|
|
|
|
|
// also, there is no persistence
|
|
|
|
// TODO persist
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var db;
|
|
|
|
|
|
|
|
function LocalStorage() {
|
|
|
|
}
|
|
|
|
db = LocalStorage;
|
|
|
|
|
|
|
|
db.prototype.getItem = function (key) {
|
2013-01-26 04:11:48 +00:00
|
|
|
if (this.hasOwnProperty(key)) {
|
2012-08-16 19:18:48 +00:00
|
|
|
return String(this[key]);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.setItem = function (key, val) {
|
|
|
|
this[key] = String(val);
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.removeItem = function (key) {
|
|
|
|
delete this[key];
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.clear = function () {
|
|
|
|
var self = this;
|
|
|
|
Object.keys(self).forEach(function (key) {
|
|
|
|
self[key] = undefined;
|
|
|
|
delete self[key];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.key = function (i) {
|
|
|
|
i = i || 0;
|
|
|
|
return Object.keys(this)[i];
|
|
|
|
};
|
|
|
|
|
|
|
|
db.prototype.__defineGetter__('length', function () {
|
|
|
|
return Object.keys(this).length;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (global.localStorage) {
|
|
|
|
module.exports = localStorage;
|
|
|
|
} else {
|
|
|
|
module.exports = new LocalStorage();
|
|
|
|
}
|
|
|
|
}());
|