json-storage.js/json-storage.js

141 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2014-01-26 21:25:42 +00:00
/*jshint -W054 */
;(function (exports) {
'use strict';
2014-01-26 21:25:42 +00:00
var proto
2012-03-02 18:35:53 +00:00
, delim = ':'
;
2014-01-26 21:25:42 +00:00
function stringify(obj) {
var str;
try {
str = JSON.stringify(obj);
} catch(e) {
str = "";
}
return str;
}
2014-01-26 21:25:42 +00:00
function parse(str) {
var obj = null;
try {
obj = JSON.parse(str);
} catch(e) {}
return obj;
}
2014-01-26 21:25:42 +00:00
function JsonStorage(w3cStorage, namespace, opts) {
var me = this
2012-03-02 18:35:53 +00:00
;
2014-01-26 21:25:42 +00:00
if (!(this instanceof JsonStorage)) {
return new JsonStorage(w3cStorage, namespace, opts);
2012-03-02 18:35:53 +00:00
}
2014-02-03 21:00:29 +00:00
if (!w3cStorage) {
w3cStorage = window.localStorage;
} else if ('function' !== typeof w3cStorage.getItem) {
throw new Error('You must supply a W3C DOM Storage mechanism such as window.localStorage or window.sessionStorage');
}
2014-01-26 21:25:42 +00:00
me._opts = opts || {};
if (false === me._opts.stringify) {
2014-01-26 21:25:42 +00:00
me._stringify = false;
} else {
me._stringify = true;
2012-03-02 18:35:53 +00:00
}
// if we didn't always add at least the delimeter
// then if a keyname with the delim, it would be more
// complicated to figure it out
this._namespace = delim;
this._namespace += (namespace || 'jss');
2014-02-24 12:27:01 +00:00
if (false === namespace) {
2014-02-24 12:23:47 +00:00
this._namespace = '';
}
2012-03-02 18:35:53 +00:00
this._store = w3cStorage;
this._keysAreDirty = true;
this._keys = [];
2012-03-02 18:35:53 +00:00
}
2014-01-26 21:25:42 +00:00
proto = JsonStorage.prototype;
2014-01-26 21:25:42 +00:00
proto.clear = function () {
2012-03-02 18:35:53 +00:00
this._keysAreDirty = true;
this.keys().forEach(function (key) {
this.remove(key);
}, this);
};
2014-01-26 21:25:42 +00:00
proto.remove = function (key) {
2012-03-02 18:35:53 +00:00
this._keysAreDirty = true;
this._store.removeItem(key + this._namespace);
};
2014-01-26 21:25:42 +00:00
proto.get = function (key) {
var item = this._store.getItem(key + this._namespace)
;
if ('undefined' === typeof item) {
item = null;
}
if (this._stringify) {
item = parse(item);
}
return item;
};
2014-01-26 21:25:42 +00:00
proto.set = function (key, val) {
2012-03-02 18:35:53 +00:00
this._keysAreDirty = true;
2014-01-26 21:25:42 +00:00
return this._store.setItem(key + this._namespace, this._stringify && stringify(val) || val);
};
2014-01-26 21:25:42 +00:00
proto.keys = function () {
2011-09-05 21:40:52 +00:00
var i
2012-03-02 18:35:53 +00:00
, key
, delimAt
2011-09-05 21:40:52 +00:00
;
2012-03-02 18:35:53 +00:00
if (!this._keysAreDirty) {
return this._keys.concat([]);
}
2012-03-02 18:35:53 +00:00
this._keys = [];
for (i = 0; i < this._store.length; i += 1) {
key = this._store.key(i) || '';
delimAt = key.lastIndexOf(this._namespace);
// test if this key belongs to this widget
2014-02-24 12:23:47 +00:00
if (!this._namespace || (-1 !== delimAt)) {
2012-03-02 18:35:53 +00:00
this._keys.push(key.substr(0, delimAt));
}
}
2012-03-02 18:35:53 +00:00
this._keysAreDirty = false;
2011-09-05 21:40:52 +00:00
2012-03-02 18:35:53 +00:00
return this._keys.concat([]);
};
2014-01-26 21:25:42 +00:00
proto.size = function () {
2012-03-02 18:35:53 +00:00
return this._store.length;
};
2014-01-26 21:25:42 +00:00
proto.toJSON = function () {
2012-03-02 18:35:53 +00:00
var json = {}
;
this.keys().forEach(function (key) {
2012-03-02 18:35:53 +00:00
json[key] = this.get(key);
}, this);
return json;
};
2014-01-26 21:25:42 +00:00
JsonStorage.create = JsonStorage;
2014-01-26 21:25:42 +00:00
exports.JsonStorage = JsonStorage;
}('undefined' !== typeof exports && exports || new Function('return this')()));