All files storage.js

94.74% Statements 54/57
84.62% Branches 44/52
100% Functions 4/4
94.74% Lines 54/57
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 1363x     2x 1x     2x 1x     1x   1x 1x       1x 1x 1x 1x   1x         3x 3x 3x         3x   13x       3x 2x 2x 2x                   20x 20x           15x 15x   15x 10x   10x 8x     2x 1x     1x     6x       2x       4x       4x 1x     3x   3x 4x 4x   4x 1x     3x     3x 3x         7x 2x   5x         4x   4x 2x   2x                  
const eventListeners = {};
 
function change (e) {
  if (!e) {
    e = window.event;
  }
 
  if (typeof e === 'undefined' || typeof e.key === 'undefined') {
    return;
  }
 
  let all = eventListeners[e.key];
 
  Eif (all) {
    all.forEach(emit);
  }
 
  function emit (listener) {
    let item = JSON.parse(e.newValue);
    let oldItem = JSON.parse(e.oldValue);
    let val = typeof item === 'object' ? item.value : item;
    let oldVal = oldItem && typeof oldItem === 'object' ? oldItem.value : oldItem;
 
    listener(val, oldVal, e.url || e.uri);
  }
}
 
class Storage {
  constructor (storage, options) {
    this.storage = storage;
    this.options = Object.assign({
      namespace: '',
      events: ['storage']
    }, options || {});
 
    Object.defineProperty(this, 'length', {
      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 (name, value, expire = null) {
    this.storage.setItem(
      this.options.namespace + name,
      JSON.stringify({value: value, expire: expire !== null ? new Date().getTime() + expire : null})
    );
  }
 
  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;
  }
 
  key (index) {
    return this.storage.key(index);
  }
 
  remove (name) {
    return this.storage.removeItem(this.options.namespace + name);
  }
 
  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]);
    }
  }
 
  on (name, callback) {
    if (eventListeners[name]) {
      eventListeners[name].push(callback);
    } else {
      eventListeners[name] = [callback];
    }
  }
 
  off (name, callback) {
    let ns = eventListeners[name];
 
    if (ns.length > 1) {
      ns.splice(ns.indexOf(callback), 1);
    } else {
      eventListeners[name] = [];
    }
  }
}
 
export {
  Storage,
  change,
  eventListeners
}