常用的代码片段收集

229 阅读1分钟

防抖函数(debounce)

// 防抖函数
const debounce = (fn, delay) => {
  let timer = null;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
};

防抖函数 (优化)

// 带是否立即执行参数 flag
const debounce = (fn, delay,flag) => {
  let timer = null;
  return (...args) => {
    clearTimeout(timer);
    let now = flag && !timer;
    timer = setTimeout(() => {
      !now && fn.apply(this, args);
      timer = null;
    }, delay);
    now && fn.apply(this, args);
  };
};

节流函数(throttle)

// 定时器方法
const throttle = (fn, delay = 500) => {
  let flag = true;
  return (...args) => {
    if (!flag) return;
    flag = false;
    setTimeout(() => {
      fn.apply(this, args);
      flag = true;
    }, delay);
  };
};
// 时间戳方法
const throttle = (fn, delay = 500) => {
  let prev = Date.now();
  return (...args) => {
    let now = Date.now();
    if(now - prev > delay){
      fn.apply(this,args);
      prev = Date.now();
    }
  };
};

转化为驼峰命名

var s1 = "get-element-by-id"

// 转化为 getElementById
var f = function(s) {
    return s.replace(/-\w/g, function(x) {
        return x.slice(1).toUpperCase();
    })
}
console.log(f(s1))      // getElementById

判断是否是电话号码正则

function isPhone(tel) {
    var regx = /^1[34578]\d{9}$/;
    return regx.test(tel);
}

验证是否是邮箱正则

function isEmail(email) {
    var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
    return regx.test(email);
}

验证是否是身份证正则

function isCardNo(number) {
    var regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return regx.test(number);
}

获取当前时间年月日时分秒

// 1. 2020-10-15
// 2. 2020-10-15 17:40:17
// 3. 15
// 4. 2020.10
// 5. 2020-10-14
export function getNowData(type) {
    let date = new Date();

    let year = date.getFullYear();

    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    let lastDay = date.getDate() - 1 < 10 ? '0' + (date.getDate() - 1) : date.getDate() - 1;
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    switch (type) {
    case 1:
        return `${year}-${month}-${day}`;
    case 2:
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    case 3:
        return day;
    case 4:
        return `${year}.${month}`;
    case 5:
        return `${year}-${month}-${lastDay}`;
    default:
        return `${year}-${month}-${day}`;
    }
}

根据身份证判断生日和性别

// 152234199308080888
export function checkIdCard(value) {
    const idCardNo = value;
    if(idCardNo.length === 18) {
        const birStr = value.substr(6, 8);
        const sexFlag = idCardNo.charAt(16) - 0; //奇数男 偶数女
        const sexfromIDcard = sexFlag % 2; //1男 0女
        return {sex: sexfromIDcard===1?0:1, birStr};
    } else if(idCardNo.length === 15) {
        const birStr = '19' + value.substr(6, 6);
        const sexFlag2 = idCardNo.charAt(14) - 0; //奇数男 偶数女
        const sexfromIDcard2 = sexFlag2 % 2; //1男 0女
        return {sex: sexfromIDcard2===1?0:1, birStr};
    }
}