made more browser-friendly
This commit is contained in:
parent
4e61155c7e
commit
699f673b79
90
lib/index.js
90
lib/index.js
@ -1,11 +1,12 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
/*jshint -W054 */
|
||||
;(function (exports) {
|
||||
'use strict';
|
||||
|
||||
var Store
|
||||
var proto
|
||||
, delim = ':'
|
||||
;
|
||||
|
||||
function Stringify(obj) {
|
||||
function stringify(obj) {
|
||||
var str;
|
||||
try {
|
||||
str = JSON.stringify(obj);
|
||||
@ -16,7 +17,7 @@
|
||||
return str;
|
||||
}
|
||||
|
||||
function Parse(str) {
|
||||
function parse(str) {
|
||||
var obj = null;
|
||||
try {
|
||||
obj = JSON.parse(str);
|
||||
@ -25,46 +26,19 @@
|
||||
return obj;
|
||||
}
|
||||
|
||||
function escapeRegExp(str) {
|
||||
return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||
}
|
||||
|
||||
function upgradeStorage(jss, w3cs) {
|
||||
var i
|
||||
, key
|
||||
, val
|
||||
, json = {}
|
||||
function JsonStorage(w3cStorage, namespace, opts) {
|
||||
var me = this
|
||||
;
|
||||
|
||||
if (jss._store.getItem('_json-storage-namespaced_', true)) {
|
||||
return;
|
||||
if (!(this instanceof JsonStorage)) {
|
||||
return new JsonStorage(w3cStorage, namespace, opts);
|
||||
}
|
||||
|
||||
// we can't modify the db while were reading or
|
||||
// the keys will shift all over the place
|
||||
for (i = 0; i < w3cs.length; i += 1) {
|
||||
key = w3cs.key(i);
|
||||
try {
|
||||
val = JSON.parse(w3cs.getItem(key));
|
||||
} catch(e) {
|
||||
return;
|
||||
}
|
||||
json[key] = val;
|
||||
}
|
||||
w3cs.clear();
|
||||
|
||||
Object.keys(json).forEach(function (key) {
|
||||
jss.set(key, json[key]);
|
||||
});
|
||||
|
||||
jss._store.setItem('_json-storage-namespaced_', true);
|
||||
}
|
||||
|
||||
function JsonStorage(w3cStorage, namespace) {
|
||||
// called without new or create
|
||||
// global will be undefined
|
||||
if (!this) {
|
||||
return new JsonStorage(w3cStorage, namespace);
|
||||
me._opts = opts || {};
|
||||
if (false === opts.stringify) {
|
||||
me._stringify = false;
|
||||
} else {
|
||||
me._stringify = true;
|
||||
}
|
||||
|
||||
// if we didn't always add at least the delimeter
|
||||
@ -76,34 +50,34 @@
|
||||
this._store = w3cStorage;
|
||||
this._keysAreDirty = true;
|
||||
this._keys = [];
|
||||
if (!this._store.getItem('_json-storage-namespaced_')) {
|
||||
upgradeStorage(this, w3cStorage);
|
||||
}
|
||||
}
|
||||
Store = JsonStorage;
|
||||
proto = JsonStorage.prototype;
|
||||
|
||||
Store.prototype.clear = function () {
|
||||
proto.clear = function () {
|
||||
this._keysAreDirty = true;
|
||||
this.keys().forEach(function (key) {
|
||||
this.remove(key);
|
||||
}, this);
|
||||
};
|
||||
|
||||
Store.prototype.remove = function (key) {
|
||||
proto.remove = function (key) {
|
||||
this._keysAreDirty = true;
|
||||
this._store.removeItem(key + this._namespace);
|
||||
};
|
||||
|
||||
Store.prototype.get = function (key) {
|
||||
return Parse(this._store.getItem(key + this._namespace));
|
||||
proto.get = function (key) {
|
||||
var item = this._store.getItem(key + this._namespace)
|
||||
;
|
||||
|
||||
return this._stringify && parse(item) || item;
|
||||
};
|
||||
|
||||
Store.prototype.set = function (key, val) {
|
||||
proto.set = function (key, val) {
|
||||
this._keysAreDirty = true;
|
||||
return this._store.setItem(key + this._namespace, Stringify(val));
|
||||
return this._store.setItem(key + this._namespace, this._stringify && stringify(val) || val);
|
||||
};
|
||||
|
||||
Store.prototype.keys = function () {
|
||||
proto.keys = function () {
|
||||
var i
|
||||
, key
|
||||
, delimAt
|
||||
@ -128,11 +102,11 @@
|
||||
return this._keys.concat([]);
|
||||
};
|
||||
|
||||
Store.prototype.size = function () {
|
||||
proto.size = function () {
|
||||
return this._store.length;
|
||||
};
|
||||
|
||||
Store.prototype.toJSON = function () {
|
||||
proto.toJSON = function () {
|
||||
var json = {}
|
||||
;
|
||||
|
||||
@ -143,9 +117,7 @@
|
||||
return json;
|
||||
};
|
||||
|
||||
Store.create = function (w3cStorage, namespace) {
|
||||
return new JsonStorage(w3cStorage, namespace);
|
||||
}
|
||||
JsonStorage.create = JsonStorage;
|
||||
|
||||
module.exports = Store;
|
||||
}());
|
||||
exports.JsonStorage = JsonStorage;
|
||||
}('undefined' !== typeof exports && exports || new Function('return this')()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user