All files index.js

58.82% Statements 40/68
50% Branches 28/56
42.86% Functions 3/7
58.82% Lines 40/68
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 1531x   1x   1x 1x 1x   1x 1x           2x                                                 1x 1x 1x       1x 1x             1x   4x           1x 1x 1x 1x 1x         1x             10x 10x           9x 9x   9x 7x   7x 6x     1x       1x     3x               2x       1x       1x 1x 1x   1x       1x                                             1x      
import Shim from './shim';
 
let listeners = {};
 
try {
  let storage = getStorage();
  let x = '__storage_test__';
 
  storage.setItem(x, x);
  storage.removeItem(x);
} catch (e) {
  throw new Error('Local storage not supported by this browser');
}
 
function getStorage () {
  return typeof window !== 'undefined' && 'localStorage' in window ? window.localStorage : Shim;
}
 
function change (e) {
  if (!e) {
    e = window.event;
  }
 
  let all = listeners[e.key];
 
  if (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 : oldItem;
    let oldVal = oldItem && typeof oldItem === 'object' ? oldItem.value : oldItem;
 
    listener(val, oldVal, e.url || e.uri);
  }
}
 
export default class VueLocalStorage {
  constructor (options) {
    this.storage = getStorage();
    this.options = Object.assign({
      namespace: ''
    }, options || {});
 
    Eif (window.addEventListener) {
      window.addEventListener('storage', change, false);
    } else if (window.attachEvent) {
      window.attachEvent('onstorage', change);
    } else {
      window.onstorage = change;
    }
 
    Object.defineProperty(this, 'length', {
      get () {
        return this.storage.length;
      }
    });
  }
 
  install (Vue, options) {
    this.options = Object.assign(this.options, options || {});
    let _this = this;
    Vue.localStorage = _this;
    Vue.ls = _this;
    Object.defineProperty(Vue.prototype, '$localStorage', {
      get () {
        return _this;
      }
    });
    Object.defineProperty(Vue.prototype, '$ls', {
      get () {
        return _this;
      }
    });
  }
 
  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;
      }
 
      Iif (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 () {
    Iif (this.length === 0) {
      return;
    }
 
    for (let i = 0; i < this.length; i++) {
      let key = this.storage.key(i);
      let regexp = new RegExp(`^${this.options.namespace}.+`, 'i');
 
      Iif (regexp.test(key) === false) {
        continue;
      }
 
      this.storage.removeItem(key);
    }
  }
 
  on (name, callback) {
    if (listeners[name]) {
      listeners[name].push(callback);
    } else {
      listeners[name] = [callback];
    }
  }
 
  off (name, callback) {
    let ns = listeners[name];
 
    if (ns.length > 1) {
      ns.splice(ns.indexOf(callback), 1);
    } else {
      listeners[name] = [];
    }
  }
}
 
Iif (typeof window !== 'undefined' && typeof window.Vue !== 'undefined') {
  window.Vue.use(new VueLocalStorage());
}