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