common-functions

自己的react+webpack+redux+antd项目 中使用的

1.清除缓存

function deleteAllCookies() {
  let cookies = document.cookie.split(';');

  for (let i = 0; i < cookies.length; i++) {
    let cookie = cookies[i];
    let eqPos = cookie.indexOf('=');
    let name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
  }
}

2.获取cookie

function getCookie(name) { /* 获取cookie*/
  let arr,
      reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg))
      return unescape(arr[2]);
  else
      return null;
}

3.值是否有效

function isValidValue(value) {
  switch (typeof (value)) {
    case 'string':
      return value === '' ? '--' : value;
    case 'number':
      return value;
    case 'undefined':
      return '--';
    case 'object':
      return value === null ? '--' : value;
    default:
      return value;
  }
}

4.数组去重

const arrayToHeavy = (array) =>  {
  const set = new Set(array);
  array = Array.from(set);
  return array;
};

5.判断对象是否是空对象

function isEmptyObj( obj ) {
  let name;
  for ( name in obj ) {
      return false;
  }
  return true;
}

6.获取url参数

/**
* 使用 let Request=new Object();
        Request=GetRequest();
        let u=Request['u'];
*/
function GetRequest(){
  let url = location.search;//获取url中?后的字串
  let theRequest = new Object();
  if(url.indexOf("?") != -1){
      let str = url.substr(1),//抽取从 start 下标开始的指定数目的字符
          strs = str.split("&");
      for(let i = 0; i < strs.length; i++){
          theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
      }
  }
  return theRequest;
}
文章目录