All files main.js

100% Statements 77/77
96.72% Branches 59/61
100% Functions 11/11
100% Lines 63/63

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  4x 4x 7x   4x       61x 61x 8x   53x 41x   12x     8x   4x     2x 1x     1x   15x 15x 15x 45x 45x 30x     45x 7x     8x     33x 5x 7x   28x 27x 27x 27x 27x 27x     27x 1x 26x 15x 27x 16x   27x 27x       8x 8x 12x   8x     6x     9x 7x 7x 22x 22x   7x       1x   1x 31x 31x 31x 27x 25x 23x 9x 9x     5x      
function getKeys(obj) {
  var names = [], name = '';
  for (name in obj) {
    names.push(name);
  }
  return names;
}
 
function isPlainObject(obj) {
  obj = JSON.stringify(obj);
  if (typeof obj !== 'string') {
    return false;
  }
  if (!/^\{[\s\S]*\}$/.test(obj)) {
    return false;
  }
  return true;
}
 
function isArray(value) { return value instanceof Array }
function toArray(value) {
  return Array.prototype.slice.call(value);
}
function Cookie(){
  if(!(this instanceof Cookie)) {
    return new Cookie()
  };
}
Cookie.prototype = {
  get: function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');//把cookie分割成组    
    for(var i=0;i < ca.length;i++) {    
      var c = ca[i];//取得字符串    
      while (c.charAt(0)==' ') {//判断一下字符串有没有前导空格    
        c = c.substring(1,c.length);//有的话,从第二位开始取    
      }    
      //如果含有我们要的name
      if (c.indexOf(nameEQ) == 0) {
        return decodeURI(c.substring(nameEQ.length,c.length));//解码并截取我们要值    
      }    
    }
    return false;
  },
  set: function(name, value, options) {
    if (isPlainObject(name)) {
      for (const k in name) {
        this.set(k, name[k], value, options);
      }
    } else if (typeof name === 'string') {
      const opt = isPlainObject(options) ? options : { expires: options },
          path = opt.path !== undefined ? `;path=${opt.path};path=/` : ';path=/',
          domain = opt.domain ? `;domain=${opt.domain}` : '',
          secure = opt.secure ? ';secure' : '';
      let expires = opt.expires !== undefined ? opt.expires : '';
 
        //过期时间
      if (typeof expires === 'string' && expires !== '') {
        expires = new Date(expires);
      } else if (typeof expires === 'number') {
        expires = new Date(+new Date + 1000 * 60 * 60 * 24 * expires);
      } if (expires !== '' && 'toGMTString' in expires) {
        expires = `;expires=${expires.toGMTString()}`;
      }
      const sameSite = opt.sameSite ? `;SameSite=${opt.sameSite}` : '';
      document.cookie = `${name}=${encodeURI(value) + expires + path + domain + secure + sameSite}`; // 转码并赋值
    }
  },
  remove: function(names) {
    names = isArray(names) ? names : toArray(arguments);
    for (var i = 0, l = names.length; i < l; i++) {
      this.set(names[i], '', -1);
    }
    return names;  
  },
  clear: function(name) {
    return name ? this.remove(name) : this.remove(getKeys(this.all()));
  },
  all: function() {
    if (document.cookie === '') return {};
    var cookies = document.cookie.split('; '),result = {};
    for (var i = 0, l = cookies.length; i < l; i++) {
      var item = cookies[i].split('=');
      result[decodeURI(item[0])] = decodeURI(item[1]);
    }
    return result;
  }
};
 
let _Cookie = null;
 
const cookie = function(name, value, options) {
  const argm = arguments;
  if (!_Cookie) _Cookie = Cookie();
  if (argm.length === 0) return _Cookie.all();
  if (argm.length === 1 && name === null) return _Cookie.clear();
  if (argm.length === 2 && !value) return _Cookie.clear(name);
  if (typeof name == 'string' && !value) return _Cookie.get(name);
  Eif ((typeof name === 'string' && value) || isPlainObject(name)) {
    return _Cookie.set(name, value, options);
  }
};
for (const a in Cookie.prototype) cookie[a] = Cookie.prototype[a];
 
export default cookie;