From 699f673b793df4652178ff9d095d0f1951b4fdad Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 26 Jan 2014 14:25:42 -0700 Subject: [PATCH] made more browser-friendly --- lib/index.js | 90 ++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 59 deletions(-) diff --git a/lib/index.js b/lib/index.js index 91b3539..8a06072 100644 --- a/lib/index.js +++ b/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')()));