JS 常用util

JS 常用util_第1张图片

在接口拦截 options.isExport,下载接口返回文件,通过a标签
JS 常用util_第2张图片

/**

  • 根据枚举组装options函数

*/

export function getSelectedOptions(enumList) {
 const array = [];
 enumList.forEach(item => {
   array.push(
     
       {item.name}
     
   );
 });
 return array;
}

/**

  • 根据id获得枚举的name

*/

export function getEnumNameById(enumList, id) {
 if (id === '' || !enumList) return null;
 const result = enumList.find(item => String(item.id) === String(id));
 return result ? result.name : null;
}

/**

  • 根据name获得枚举的id

*/

export function getEnumIdByName(enumList, name) {
 if (!name || !enumList) return null;
 const result = enumList.find(item => String(item.name) === String(name));
 return result ? result.id : null;
}

/**

  • 获取数组对象中某个属性

*/

export function getArrayProps(array, key) {
 const keys = key || 'id';
 const res = [];
 if (array) {
   array.forEach(t => {
     res.push(t[keys]);
   });
 }
 return res;
}

/**

  • 去除参数 前后空格

*/

export function paramsTrim(params) {
 const values = {};
 for (const key in params) {
   if ({}.hasOwnProperty.call(params, key)) {
     if (typeof params[key] === 'string') {
       values[key] = params[key].trim();
     } else {
       values[key] = params[key];
     }
   }
 }
 return values;
}

/**

  • 获取地址栏参数

*/

export function getUrlPrmt(url) {
  const tempUrl = url || window.location.href || '';
  const pa = tempUrl.substring(tempUrl.indexOf('?') + 1);
  const arrS = pa.split('&');
  const rs = {};
  for (let i = 0, len = arrS.length; i < len; i += 1) {
    const pos = arrS[i].indexOf('=');
    if (pos !== -1) {
      const name = arrS[i].substring(0, pos);
      const value = window.decodeURIComponent(arrS[i].substring(pos + 1));
      rs[name] = value;
    }
  }
  return rs;
}

/**

  • 判断对象是否含有该属性

*/

export function isHas(obj, key) {
   const isObj =
     Object.prototype.toString.call(obj) === '[object Object]' && obj.constructor === Object;
   if (key === null || key === undefined || isObj === false) {
     return isObj;
   }
   let isHaskey = false;
   if (isObj && Object.prototype.hasOwnProperty.call(obj, key) && obj !== undefined) {
     isHaskey = obj[key] !== null && obj[key] !== undefined;
   } else {
     isHaskey = false;
   }
   return isHaskey;
 }

/**

  • 判断是否为数组

*/

export function isArrayFn(value) {
   if (typeof Array.isArray === 'function') {
     return Array.isArray(value) && value.length > 0;
   }
   return Object.prototype.toString.call(value) === '[object Array]' && value.length > 0;
 }
 
 export default isArrayFn;

/**

  • 生成uuid

*/

export function uuidGenerate() {
 const s = [];
 const hexDigits = '0123456789abcdef';
 for (let i = 0; i < 36; i += 1) {
   s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
   if ([8, 13, 18, 25].includes(i)) {
     s[i] = '-';
   }
 }
 s[14] = '4';
 s[19] = hexDigits.substr((s[19] && 0x3) || 0x8, 1);
 const uuid = s.join('');
 return uuid;
}

你可能感兴趣的