js开发添加的一些工具方法

 1. 判断当前所在的环境:

/*
 * 判断运行环境
 */
const _uas = navigator.userAgent.toLowerCase();
const isWho = {
    is_weixin: function() {
        if (_uas.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        } else {
            return false;
        }
    },
    is_ios: function() {
        if (_uas.match(/iphone/i) || _uas.match(/ipad/i) || _uas.match(/ipod/i)) {
            return true;
        } else {
            return false;
        }
    },
    is_android: function() {
        if (_uas.match(/android/i)) {
            return true;
        } else {
            return false;
        }
    },
    is_weibo: function() {
        if (_uas.match(/weibo/i)) {
            return true;
        } else {
            return false;
        }
    }
}
View Code

  获取特定url参数

/*
 * URL获取参数
 * 
 * @param {String} name 要获取的参数名称
 * @return {String} 获取的参数值
 */
const getUrl = function(name) {
    // console.log(decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ''])[1]));
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ''])[1]
    );
}
View Code

  数组对象深拷贝

    this.alldeepCopy = function(o) {
        if (o instanceof Array) {
            var n = [];
            for (var i = 0; i < o.length; ++i) {
                n[i] = this.alldeepCopy(o[i]);
            }
            return n;

        } else if (o instanceof Object) {
            var n = {}
            for (var i in o) {
                n[i] = this.alldeepCopy(o[i]);
            }
            return n;
        } else {
            return o;
        }
    }

 2.  js处理浮点数的加减乘除工具方法

 3. js拷贝到粘贴板

    input的拷贝

function copyText() {
 var elem = document.getElementById(id);
 
  elem.select();
  document.execCommand("Copy");

  alert("Copied the text);
}

  拷贝其他标签的文本,如span

function CopyToClipboard(containerid) {
    var range = document.createRange();
     range.selectNode(document.getElementById(containerid));
     window.getSelection().addRange(range);
     document.execCommand("copy");
     alert("text copied") ;
}

 4. 对各种时间格式转换

//时间戳转时间
function stampToTime(dateNum: any, type, defaultVal = "- -") {
    if (!dateNum) {
        if (dateNum !== 0) {
            return defaultVal
        }
    }
    if (typeof (dateNum) == "string") {
        if(dateNum.indexOf('-') == -1){
            if (dateNum.length == 13) {
                dateNum = parseInt(dateNum);
            } else {
                return dateNum
            }
        }
    }
    let date = new Date(dateNum);
    let month = date.getMonth() + 1;
    if(type == 1){ //2018-5-5 12:07:05
        return date.getFullYear() + "-" + month + "-" + date.getDate() + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
    };
    if(type == 2){ //2018-05-05 12:07:05
        return date.getFullYear() + "-" + fixZero(month, 2) + "-" + fixZero(date.getDate(), 2) + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
    };
    if(type == 3){ //2018/5/5 12:07:05
        return date.getFullYear() + "/" + month + "/" + date.getDate() + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
    };
    if(type == 4){ //2018/05/05 12:07:05
        return date.getFullYear() + "/" + fixZero(month, 2) + "/" + fixZero(date.getDate(), 2) + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
    };
    if(type == 5){ //2018-5-5
        return date.getFullYear() + "-" + month + "-" + date.getDate()
    };
    if(type == 6){ //2018-05-05
        return date.getFullYear() + "-" + fixZero(month, 2) + "-" + fixZero(date.getDate(), 2)
    };
    if(type == 7){ //2018/5/5
        return date.getFullYear() + "/" + month + "/" + date.getDate()
    };
    if(type == 8){ //2018/05/05
        return date.getFullYear() + "/" + fixZero(month, 2) + "/" + fixZero(date.getDate(), 2)
    };
    if(type == 9){ //2018年05月05
        return date.getFullYear() + "年" + fixZero(month, 2) + "月" + fixZero(date.getDate(), 2) + "日"
    };
    if(type == 10){ //20180505120705
        return date.getFullYear() + fixZero(month, 2) + fixZero(date.getDate(), 2) + fixZero(date.getHours(), 2) + fixZero(date.getMinutes(), 2) + fixZero(date.getSeconds(), 2)
    };
}

 5. js hex String和 byteArray转换

// Convert a hex string to a byte array
function hexToBytes(hex) {
    for (var bytes = [], c = 0; c < hex.length; c += 2)
    bytes.push(parseInt(hex.substr(c, 2), 16));
    return bytes;
}

// Convert a byte array to a hex string
function bytesToHex(bytes) {
    for (var hex = [], i = 0; i < bytes.length; i++) {
        var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
        hex.push((current >>> 4).toString(16));
        hex.push((current & 0xF).toString(16));
    }
    return hex.join("");
}

猜你喜欢

转载自www.cnblogs.com/johnzhu/p/5970481.html