All files main.js

89.65% Statements 104/116
91.8% Branches 56/61
82.6% Functions 19/23
88.17% Lines 82/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158  40x 40x 24x   16x       23x     196x 185x 7x   4x 4x       1x 1x   1x 1x   1x 1x                                   1x 1x       1x     2x 1x       1x   29x 23x 6x 9x   29x       241x 41x 133x 41x   200x 10x   190x 190x 3x 3x 9x 9x 5x     3x   187x     4x 4x     8x 8x 8x   31x   5x 5x 10x   5x     49x 151x 151x   49x     2x 2x 4x   2x     2x       1x   39x 39x 39x 39x 35x 27x 5x   9x 7x 4x 3x 2x 2x 2x     5x 1x 2x 2x     5x   9x      
function isJSON(obj) {
  obj = JSON.stringify(obj);
  if (!/^\{[\s\S]*\}$/.test(obj)) {
    return false;
  }
  return true;
}
 
function stringify(val) {
  return val === undefined || typeof val === "function" ? val + '' : JSON.stringify(val);
}
function deserialize(value) {
  if (typeof value !== 'string') { return undefined; }
  try { return JSON.parse(value); }
  catch (e) { return value; }
}
function isFunction(value) { return ({}).toString.call(value) === "[object Function]"; }
function isArray(value) { return Object.prototype.toString.call(value) === "[object Array]"; }
// https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError
function dealIncognito(storage) {
  var _KEY = '_Is_Incognit', _VALUE = 'yes';
  try {
    // NOTE: set default storage when not passed in
    Eif (!storage) {
      storage = window.localStorage;
    }
    storage.setItem(_KEY, _VALUE);
    storage.removeItem(_KEY);
  } catch (e) {
    var inMemoryStorage = {};
    inMemoryStorage._data = {};
    inMemoryStorage.setItem = function (id, val) {
      return inMemoryStorage._data[id] = String(val);
    };
    inMemoryStorage.getItem = function (id) {
      return inMemoryStorage._data.hasOwnProperty(id) ? inMemoryStorage._data[id] : undefined;
    };
    inMemoryStorage.removeItem = function (id) {
      return delete inMemoryStorage._data[id];
    };
    inMemoryStorage.clear = function () {
      return inMemoryStorage._data = {};
    }
    storage = inMemoryStorage;
  }
  finally { Iif (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); }
  return storage;
}
 
// deal QuotaExceededError if user use incognito mode in browser
const storage = dealIncognito();
 
function Store() {
  if (!(this instanceof Store)) {
    return new Store();
  }
}
 
Store.prototype = {
  set: function (key, val) {
    if (key && !isJSON(key)) {
      storage.setItem(key, stringify(val));
    } else Eif (isJSON(key)) {
      for (var a in key) this.set(a, key[a]);
    }
    return this;
  },
  get: function (key) {
    // Return all entries if no key
    if (key === undefined) {
      var ret = {};
      this.forEach((key, val) => ret[key] = val);
      return ret;
    }
    if (key.charAt(0) === '?') {
      return this.has(key.substr(1));
    }
    const args = arguments;
    if (args.length > 1) {
      const dt = {};
      for (var i = 0, len = args.length; i < len; i++) {
        const value = deserialize(storage.getItem(args[i]));
        if (this.has(args[i])) {
          dt[args[i]] = value;
        }
      }
      return dt;
    }
    return deserialize(storage.getItem(key));
  },
  clear: function () {
    storage.clear();
    return this;
  },
  remove: function (key) {
    var val = this.get(key);
    storage.removeItem(key);
    return val;
  },
  has: function (key) { return ({}).hasOwnProperty.call(this.get(), key); },
  keys: function () {
    var d = [];
    this.forEach((k) => {
      d.push(k);
    });
    return d;
  },
  forEach: function (callback) {
    for (var i = 0, len = storage.length; i < len; i++) {
      var key = storage.key(i);
      callback(key, this.get(key));
    }
    return this;
  },
  search: function (str) {
    var arr = this.keys(), dt = {};
    for (var i = 0, len = arr.length; i < len; i++) {
      if (arr[i].indexOf(str) > -1) dt[arr[i]] = this.get(arr[i]);
    }
    return dt;
  },
  len: function () {
    return storage.length;
  }
}
 
let _Store = null;
function store(key, data) {
  const argm = arguments;
  let dt = null;
  if (!_Store) _Store = Store();
  if (argm.length === 0) return _Store.get();
  if (argm.length === 1) {
    if (typeof (key) === "string") return _Store.get(key);
    if (isJSON(key)) return _Store.set(key);
  }
  if (argm.length === 2 && typeof (key) === "string") {
    if (!data) return _Store.remove(key);
    if (data && typeof (data) === "string") return _Store.set(key, data);
    if (data && isFunction(data)) {
      dt = null
      dt = data(key, _Store.get(key))
      store.set(key, dt);
    }
  }
  if (argm.length === 2 && isArray(key) && isFunction(data)) {
    for (var i = 0, len = key.length; i < len; i++) {
      dt = data(key[i], _Store.get(key[i]))
      store.set(key[i], dt)
    }
  }
  return store
}
for (var a in Store.prototype) store[a] = Store.prototype[a];
 
export default store;