js 处理方法技巧

//取路径参数        
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i"); var separator = uri.indexOf('?') !== -1 ? "&" : "?"; if (uri.match(re)) { return uri.replace(re, '$1' + key + "=" + value + '$2'); } else { return uri + separator + key + "=" + value; } }
/**
 * 判断浏览器版本
 * @param 
 */
export function myBrowser() {
  var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 
  var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器 
  // var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器 
  var isIE = window.ActiveXObject || "ActiveXObject" in window
  // var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; //判断是否IE的Edge浏览器 
  var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
  var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器 
  var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器 
  var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1 && !isEdge; //判断Chrome浏览器 

  if (isIE) {
    var reIE = new RegExp("MSIE (\d+\.\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
    if (userAgent.indexOf('MSIE 6.0') != -1) {
      return "IE6";
    } else if (fIEVersion == 7) {
      return "IE7";
    } else if (fIEVersion == 8) {
      return "IE8";
    } else if (fIEVersion == 9) {
      return "IE9";
    } else if (fIEVersion == 10) {
      return "IE10";
    } else if (userAgent.toLowerCase().match(/rv:([d.]+)) like gecko/)) {
      return "IE11";
    } else {
      return "0"
    } //IE版本过低
  } //isIE end 

  if (isFF) {
    return "FF";
  }
  if (isOpera) {
    return "Opera";
  }
  if (isSafari) {
    return "Safari";
  }
  if (isChrome) {
    return "Chrome";
  }
  if (isEdge) {
    return "Edge";
  }

}

/**
 * 导出二进制流
 * @param name 小区名
 */
export function downloadBlob(res, name){
  const blob = new Blob([res], {
    type: "text/csv;charset=utf-8"
  });
  const fileName =  `${name}.csv`;
  let mb = myBrowser();
  if ("download" in document.createElement("a") && mb.search("IE") == -1 && mb != 'Edge') {
    // 非IE下载
    const elink = document.createElement("a");
    elink.download = fileName;
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }

}
// 数组去交集
this.ccListArr.filter(item => !t.temporary.some(ele=>ele.id===item.id)); //去两个对象数值不同的元素
finally方法用于指定不管 Promise 对象最后状态如何,都会执行的操作。它与done方法的最大区别,它接受一个普通的回调函数作为参数,该函数不管怎样都必须执行。
done()Promise 对象的回调链,不管以then方法或catch方法结尾,要是最后一个方法抛出错误,都有可能无法捕捉到(因为 Promise 内部的错误不会冒泡到全局)。因此,我们可以提供一个done方法,总是处于回调链的尾端,保证抛出任何可能出现的错误。

/* eslint-disable */
Promise.prototype.finally = function(callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason =>
      P.resolve(callback()).then(() => {
        throw reason;
      })
  );
};
Promise.prototype.done = function(onFulfilled, onRejected) {
  this.then(onFulfilled, onRejected).catch(function(reason) {
    // 抛出一个全局错误
    setTimeout(() => {
      throw reason;
    }, 0);
  });
};
/* eslint-disable */
//返回的键值首字母转小写
export function keyToLowerCase (jsonObj) { let arr = [] if (Object.prototype.toString.call(jsonObj) === '[object Array]') { jsonObj.forEach(item => { if (Object.prototype.toString.call(item) === '[object Object]') { for (var key in item) { item[key.substring(0, 1).toLowerCase() + key.substring(1)] = item[key] if (Object.prototype.toString.call(item[key]) === '[object Array]') { keyToLowerCase(item[key]) } delete item[key] } arr.push(item) } }) return arr } }
原文地址:https://www.cnblogs.com/zzghk/p/10529298.html