local-storage.js/lib/localStorage.js

55 lines
1.1 KiB
JavaScript

// 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) {
if (this.hasOwnProperty(key)) {
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();
}
}());