All files storage.js

94.34% Statements 50/53
89.13% Branches 41/46
100% Functions 4/4
94.34% Lines 50/53
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 1903x               3x 1x     3x 1x     2x   2x 1x       1x                       3x 3x 3x         3x             13x       3x 2x 2x 2x                                 20x 20x                         15x 15x   15x 10x   10x 8x     2x 1x     1x     6x                   3x                   4x             4x 1x     3x   3x 4x 4x   4x 1x     3x     3x 3x                     7x 2x   5x                     4x   4x 2x   2x                    
const eventListeners = {};
 
/**
 * Event callback
 *
 * @param {Object} e
 */
function change (e) {
  if (!e) {
    e = window.event;
  }
 
  if (typeof e === 'undefined' || typeof e.key === 'undefined') {
    return;
  }
 
  let all = eventListeners[e.key];
 
  if (typeof all !== 'undefined') {
    all.forEach(emit);
  }
 
  function emit (listener) {
    listener(e.newValue ? JSON.parse(e.newValue).value : e.newValue, e.oldValue ? JSON.parse(e.oldValue).value : e.oldValue, e.url || e.uri);
  }
}
 
/**
 * Storage Bridge
 */
class Storage {
  /**
   * @param {Object} storage
   * @param {Object} options
   */
  constructor (storage, options) {
    this.storage = storage;
    this.options = Object.assign({
      namespace: '',
      events: ['storage']
    }, options || {});
 
    Object.defineProperty(this, 'length', {
      /**
       * Define length property
       *
       * @return {number}
       */
      get () {
        return this.storage.length;
      }
    });
 
    if (typeof window !== 'undefined') {
      for (let i in this.options.events) {
        Eif (window.addEventListener) {
          window.addEventListener(this.options.events[i], change, false);
        } else if (window.attachEvent) {
          window.attachEvent(`on${this.options.events[i]}`, change);
        } else {
          window[`on${this.options.events[i]}`] = change;
        }
      }
    }
  }
 
  /**
   * Set item
   *
   * @param {string} name
   * @param {*} value
   * @param {number} expire - seconds
   */
  set (name, value, expire = null) {
    this.storage.setItem(
      this.options.namespace + name,
      JSON.stringify({value: value, expire: expire !== null ? new Date().getTime() + expire : null})
    );
  }
 
  /**
   * Get item
   *
   * @param {string} name
   * @param {*} def - default value
   * @returns {*}
   */
  get (name, def = null) {
    let item = this.storage.getItem(this.options.namespace + name);
 
    if (item !== null) {
      let data = JSON.parse(item);
 
      if (data.expire === null) {
        return data.value;
      }
 
      if (data.expire >= new Date().getTime()) {
        return data.value;
      }
 
      this.remove(name);
    }
 
    return def;
  }
 
  /**
   * Get item by key
   *
   * @param {number} index
   * @return {*}
   */
  key (index) {
    return this.storage.key(index);
  }
 
  /**
   * Remove item
   *
   * @param {string} name
   * @return {boolean}
   */
  remove (name) {
    return this.storage.removeItem(this.options.namespace + name);
  }
 
  /**
   * Clear storage
   */
  clear () {
    if (this.length === 0) {
      return;
    }
 
    let removedKeys = [];
 
    for (let i = 0; i < this.length; i++) {
      let key = this.storage.key(i);
      let regexp = new RegExp(`^${this.options.namespace}.+`, 'i');
 
      if (regexp.test(key) === false) {
        continue;
      }
 
      removedKeys.push(key);
    }
 
    for (let key in removedKeys) {
      this.storage.removeItem(removedKeys[key]);
    }
  }
 
  /**
   * Add storage change event
   *
   * @param {string} name
   * @param {Function} callback
   */
  on (name, callback) {
    if (eventListeners[this.options.namespace + name]) {
      eventListeners[this.options.namespace + name].push(callback);
    } else {
      eventListeners[this.options.namespace + name] = [callback];
    }
  }
 
  /**
   * Remove storage change event
   *
   * @param {string} name
   * @param {Function} callback
   */
  off (name, callback) {
    let ns = eventListeners[this.options.namespace + name];
 
    if (ns.length > 1) {
      ns.splice(ns.indexOf(callback), 1);
    } else {
      eventListeners[this.options.namespace + name] = [];
    }
  }
}
 
export {
  Storage,
  change,
  eventListeners
};