| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- (function(root, factory) {
- if (typeof exports !== 'undefined') {
- if (typeof module !== 'undefined' && module.exports) {
- exports = module.exports = factory(root, exports);
- }
- } else if (typeof define === 'function' && define.amd) {
- define(['exports'], function(exports) {
- root.Lockr = factory(root, exports);
- });
- } else {
- root.Lockr = factory(root, {});
- }
- }(this, function(root, Lockr) {
- 'use strict';
- if (!Array.prototype.indexOf) {
- Array.prototype.indexOf = function(elt /*, from*/)
- {
- var len = this.length >>> 0;
- var from = Number(arguments[1]) || 0;
- from = (from < 0)
- ? Math.ceil(from)
- : Math.floor(from);
- if (from < 0)
- from += len;
- for (; from < len; from++)
- {
- if (from in this &&
- this[from] === elt)
- return from;
- }
- return -1;
- };
- }
- Lockr.prefix = "";
- Lockr._getPrefixedKey = function(key, options) {
- options = options || {};
- if (options.noPrefix) {
- return key;
- } else {
- return this.prefix + key;
- }
- };
- Lockr.set = function (key, value, options) {
- var query_key = this._getPrefixedKey(key, options);
- try {
- localStorage.setItem(query_key, JSON.stringify({"data": value}));
- } catch (e) {
- if (console) console.warn("Lockr didn't successfully save the '{"+ key +": "+ value +"}' pair, because the localStorage is full.");
- }
- };
- Lockr.get = function (key, missing, options) {
- var query_key = this._getPrefixedKey(key, options),
- value;
- try {
- value = JSON.parse(localStorage.getItem(query_key));
- } catch (e) {
- if(localStorage[query_key]) {
- value = {data: localStorage.getItem(query_key)};
- } else{
- value = null;
- }
- }
-
- if(!value) {
- return missing;
- }
- else if (typeof value === 'object' && typeof value.data !== 'undefined') {
- return value.data;
- }
- };
- Lockr.sadd = function(key, value, options) {
- var query_key = this._getPrefixedKey(key, options),
- json;
- var values = Lockr.smembers(key);
- if (values.indexOf(value) > -1) {
- return null;
- }
- try {
- values.push(value);
- json = JSON.stringify({"data": values});
- localStorage.setItem(query_key, json);
- } catch (e) {
- console.log(e);
- if (console) console.warn("Lockr didn't successfully add the "+ value +" to "+ key +" set, because the localStorage is full.");
- }
- };
- Lockr.smembers = function(key, options) {
- var query_key = this._getPrefixedKey(key, options),
- value;
- try {
- value = JSON.parse(localStorage.getItem(query_key));
- } catch (e) {
- value = null;
- }
-
- return (value && value.data) ? value.data : [];
- };
- Lockr.sismember = function(key, value, options) {
- return Lockr.smembers(key).indexOf(value) > -1;
- };
- Lockr.keys = function() {
- var keys = [];
- var allKeys = Object.keys(localStorage);
- if (Lockr.prefix.length === 0) {
- return allKeys;
- }
- allKeys.forEach(function (key) {
- if (key.indexOf(Lockr.prefix) !== -1) {
- keys.push(key.replace(Lockr.prefix, ''));
- }
- });
- return keys;
- };
- Lockr.getAll = function (includeKeys) {
- var keys = Lockr.keys();
- if (includeKeys) {
- return keys.reduce(function (accum, key) {
- var tempObj = {};
- tempObj[key] = Lockr.get(key);
- accum.push(tempObj);
- return accum;
- }, []);
- }
- return keys.map(function (key) {
- return Lockr.get(key);
- });
- };
- Lockr.srem = function(key, value, options) {
- var query_key = this._getPrefixedKey(key, options),
- json,
- index;
- var values = Lockr.smembers(key, value);
- index = values.indexOf(value);
- if (index > -1)
- values.splice(index, 1);
- json = JSON.stringify({"data": values});
- try {
- localStorage.setItem(query_key, json);
- } catch (e) {
- if (console) console.warn("Lockr couldn't remove the "+ value +" from the set "+ key);
- }
- };
- Lockr.rm = function (key) {
- var queryKey = this._getPrefixedKey(key);
-
- localStorage.removeItem(queryKey);
- };
- Lockr.flush = function () {
- if (Lockr.prefix.length) {
- Lockr.keys().forEach(function(key) {
- localStorage.removeItem(Lockr._getPrefixedKey(key));
- });
- } else {
- localStorage.clear();
- }
- };
- return Lockr;
- }));
|