javascript工具函数

69 阅读2分钟

1. 横杆(-) 连接的字符串转成驼峰标识的字符串

get-age 转成 getAge

function camelize(str) {
    return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')    
}

扩展知识

String.prototype.replace 第二个参数可以是一个回调方法,正则匹配几次就回调几次。回调方法的参数跟正则的分组有关

// 有()分组,就有4个有效入参
'get-age'.replace(/-(\w+)/g, function(_, c, d, e) {
    console.log(_)  // -age 匹配到的全部数据
    console.log(c)  // age  匹配到的除了特殊符号外的字符串数据
    console.log(d)  // 3    匹配到的数据的下标值
    console.log(e)  // get-age  需要匹配的原始字符串
})
// 没有()分组就只有3个有效入参
'get-age'.replace(/age/g, function(_, c, d, e) {
    console.log(_) // age 匹配到的字符串数据
    console.log(c) // 4   匹配到的数据的下标值
    console.log(d) // get-age  需要匹配的原始字符串
    console.log(e) // undefined  没有数据 undefined
})

2.判断是否是一个对象

function isPlainObject (obj) {
    return Object.prototype.toString.call(obj) === '[object Object]'
}

3.javascript计算

const calculate = {
    // -
    minus: function(n,m){
        n=typeof n =="string"?n:this.numToString(n);
        m=typeof m =="string"?m:this.numToString(m);
        var F= n.indexOf(".")!=-1?this.handleNum(n):[n,0,0],
            S= m.indexOf(".")!=-1?this.handleNum(m):[m,0,0],
            l1=F[2],
            l2=S[2],
            L=l1>l2?l1:l2,
            T=Math.pow(10,L);
        return (F[0]*T+F[1]*T/Math.pow(10,l1)-S[0]*T-S[1]*T/Math.pow(10,l2))/T
    },
    // 
    multiply:function(n,m){
        n=typeof n =="string"?n:this.numToString(n);
        m=typeof m =="string"?m:this.numToString(m);
        var F= n.indexOf(".")!=-1?this.handleNum(n):[n,0,0],
            S= m.indexOf(".")!=-1?this.handleNum(m):[m,0,0],
            l1=F[2],
            l2=S[2],
            L=l1>l2?l1:l2,
            T=Math.pow(10,L);
        return ((F[0]*T+F[1]*T/Math.pow(10,l1))*(S[0]*T+S[1]*T/Math.pow(10,l2)))/T/T
    },
    // /
    division:function(n,m){
        n=typeof n =="string"?n:this.numToString(n);
        m=typeof m =="string"?m:this.numToString(m);
        var F= n.indexOf(".")!=-1?this.handleNum(n):[n,0,0],
            S= m.indexOf(".")!=-1?this.handleNum(m):[m,0,0],
            l1=F[2],
            l2=S[2],
            L=l1>l2?l1:l2,
            T=Math.pow(10,L);
        return ((F[0]*T+F[1]*T/Math.pow(10,l1))/(S[0]*T+S[1]*T/Math.pow(10,l2)))
    },
    numToString:function(tempArray){
        if(Object.prototype.toString.call(tempArray) == "[object Array]"){
            var temp=tempArray.slice();
            for(var i,l=temp.length;i<l;i++){
                temp[i]=typeof temp[i] == "number"?temp[i].toString():temp[i];
            }
            return temp;
        }
        if(typeof tempArray=="number"){
            return tempArray.toString();
        }
        return []
    },
    plus:function(n,m){
        n=typeof n =="string"?n:this.numToString(n);
        m=typeof m =="string"?m:this.numToString(m);
        var F= n.indexOf(".")!=-1?this.handleNum(n):[n,0,0],
            S= m.indexOf(".")!=-1?this.handleNum(m):[m,0,0],
            l1=F[2],
            l2=S[2],
            L=l1>l2?l1:l2,
            T=Math.pow(10,L);
        return (F[0]*T+F[1]*T/Math.pow(10,l1)+S[0]*T+S[1]*T/Math.pow(10,l2))/T
    },
    handleNum:function(n){
        n=typeof n !=="string"?n+"":n;
        var temp= n.split(".");
        temp.push(temp[1].length);
        return temp
    }
}

4.数字分隔符

1234567.89 转化成 1,234,567.89

function numberFormat(num) {
    return !isNaN(Number(num)) ? Number(num).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',') : '0.00';
}