美文网首页
js 函数工具集

js 函数工具集

作者: _theFeng | 来源:发表于2019-05-10 10:18 被阅读0次
//  获取url的参数
const  getUrlParams=()=>{
var q={};
location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=unescape(v));
return q;
}
//console.log(q)
// 格式化金钱  银行卡的数据类型
const ThousandNum = num => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//const money = ThousandNum(19941112);
// money => "19,941,112"
// 生成随机ID
const RandomId = len => Math.random().toString(36).substr(3, len);
//const id = RandomId(10);
// id => "jg7zpgiqva"
// 生成范围随机数
const RandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
//const num = RandomNum(1, 10);
// 生成随机颜色
const RandomColor = () => "#" + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, "0");
//const color = RandomColor();
// color => "#f03665"
// 生成星级评价
const StartScore = rate => "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);
//const start = StartScore(3);
// start => "★★★"
// 数值前补0
const FillZero = (num, len) => num.toString().padStart(len, "0");
//const num = FillZero(169, 5);
// num => "00169"
// 精确小数
const RoundNum = (num, decimal) => Math.round(num * 10 ** decimal) / 10 ** decimal;
//const num = RoundNum(1.69, 1);
// num => 1.7
// 精确几位小数
// 判断数据类型最全集
const DataType=(tgt, type)=> {
    const dataType = Object.prototype.toString.call(tgt).replace(/\[object /g, "").replace(/\]/g, "").toLowerCase();
    return type ? dataType === type : dataType;
}
//DataType("yajun"); // "string"
//DataType(19941112); // "number"
//DataType(true); // "boolean"
//DataType([], "array"); // true
//DataType({}, "array"); // false
// 混淆数组
const confusionArray= arr => [0, 1, 2, 3, 4, 5].slice().sort(() => Math.random() - .5);
// 过滤数组全部undefined、null、""、0、false、NaN
const filterAll = arr =>afrr.filter(Boolean)
// 统计数据的成员
const getArrCount = arr=>arr.reduce((t, c) => {
    t[c] = t[c] ? ++ t[c] : 1;
    return t;
}, {});
//getArrCount( [0, 1, 1, 2, 2, 2];)
// {0: 1, 1: 2, 2: 3}
//获取数组随机成员
const randomItem = arr=>arr[Math.floor(Math.random() * arr.length)];
// 深拷贝(支持多层嵌套)
const deepCopy = (data) => {
  const t = Utils.typeOf(data);
  let o;

  if (t === 'array') {
    o = [];
  } else if (t === 'object') {
    o = {};
  } else {
    return data;
  }

  if (t === 'array') {
    for (let i = 0, len = data.length; i < len; i++) {
      o.push(Utils.deepCopy(data[i]));
    }
  } else if (t === 'object') {
    // eslint-disable-next-line
    for (const i in data) {
      if (Object.prototype.hasOwnProperty.call(data, i)) {
        o[i] = Utils.deepCopy(data[i]);
      }
    }
  }
  return o;
};
//  删除对象中  null  undefined  '' 的属性
const objRemoveNull = (obj) => {
  const typeOf = (obj) => {
  // eslint-disable-next-line
  const toString = Object.prototype.toString;
  const map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object',
  };
  return map[toString.call(obj)];
};
  if (typeOf(obj) !== 'object') {
    return {};
  }
  const result = Utils.deepCopy(obj);
  Object.entries(result).forEach((item) => {
    // eslint-disable-next-line
    if (item[1] == null || item[1] == undefined || item[1] === '') {
      delete result[item[0]];
    }
  });
  return result;
};
//校验手机号 座机  分机号码
const validPhoneNumber = (phoneNumber) => {
  const isMobile = /^1(3|4|5|6|7|8|9)[0-9]\d{8}$/;
  const isPhone = /^(0\d{2,3}-?)?\d{7,8}$/;
  const isExt = /^[48]00-?\d{3}-?\d{4}$/;
  if (isMobile.test(phoneNumber)) {
    return true;
  }
  if (isPhone.test(phoneNumber)) {
    return true;
  }
  if (isExt.test(phoneNumber)) {
    return true;
  }
  return false;
};
// 校验身份证
const validIDCard = (cid) => {
  const arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  const arrValid = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  if (/^\d{17}\d|x$/i.test(cid)) {
    let sum = 0;

    for (let i = 0; i < cid.length - 1; i++) {
      // 对前17位数字与权值乘积求和
      sum += parseInt(cid.substr(i, 1), 10) * arrExp[i];
    }
    // 计算模(固定算法)
    const idx = sum % 11;
    // 检验第18为是否与校验码相等
    return `${arrValid[idx]}` === cid.substr(17, 1).toUpperCase();
  }
  return false;
};
//  通过身份证判断年龄
const getAgeByIdCard = (idcard) => {
  if (!this.validIDCard(idcard)) {
    return null;
  }

  return (new Date()).getFullYear() - parseInt(idcard.substring(6, 10), 10);
};

相关文章

网友评论

      本文标题:js 函数工具集

      本文链接:https://www.haomeiwen.com/subject/edeooqtx.html