JavaScript的常用工具方法大全持续更新中...

Posted 码上暴富

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript的常用工具方法大全持续更新中...相关的知识,希望对你有一定的参考价值。

javascript的常用工具类大全【持续更新中...】

JavaScript的常用工具类1(校验邮箱格式、校验手机格式、校验身份证号、截取字段长度、求和)

//校验邮箱格式
function checkEmail(email) 
	return RegExp(/^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]2,3$/).test(
		email);

//校验手机格式
function checkMobile(mobile) 
	let phoneReg = /(^1[3|4|5|6|7|8|9]\\d9$)|(^09\\d8$)/;
	return phoneReg.test(mobile)


/* 校验身份证号  */
function checkIdCard(id) 
	var flag = true;
	//转换大小写
	id = id.toUpperCase();
	var arrVerifyCode = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
	var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
	var Checker = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1];
	if (id.length != 15 && id.length != 18) 
		flag = false;
	
	var Ai = id.length == 18 ? id.substring(0, 17) : id.slice(0, 6) + "19" + id.slice(6, 16);
	if (!/^\\d+$/.test(Ai)) 
		flag = false;
	
	var yyyy = Ai.slice(6, 10),
		mm = Ai.slice(10, 12) - 1,
		dd = Ai.slice(12, 14);
	var d = new Date(yyyy, mm, dd),
		now = new Date();
	var year = d.getFullYear(),
		mon = d.getMonth(),
		day = d.getDate();
	if (year != yyyy || mon != mm || day != dd || d > now || year < 1800) 
		flag = false;
	
	for (var i = 0, ret = 0; i < 17; i++) 
		ret += Ai.charAt(i) * Wi[i]
	
	Ai += arrVerifyCode[ret %= 11];
	if (id.length == 18 && id != Ai) 
		flag = false;
	
	return flag;


//截取字段长度
function strNum(str, num) 
	let data = str
	if (str.length > num) 
		data = str.substring(0, num)
	
	return data

//求和
let sum3 = (...m) => 
	let total = 0;
	for (var i of m) 
		total += i
	
	return `$total`
;

module.exports = 
	checkEmail: checkEmail,
	checkMobile: checkMobile,
	strNum: strNum,
	checkIdCard: checkIdCard


JavaScript的常用工具类1(解决浮动运算、小数转成整数、检测数字是否越界、精确加减乘除法、四舍五入)

'use strict';

Object.defineProperty(exports, '__esModule',  value: true );

/**
 * @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
 * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
 */
/**
 * 把错误的数据转正
 * strip(0.09999999999999998)=0.1
 */
function strip(num, precision) 
    if (precision === void 0)  precision = 12; 
    return +parseFloat(num.toPrecision(precision));

/**
 * Return digits length of a number
 * @param *number num Input number
 */
function digitLength(num) 
    // Get digit length of e
    var eSplit = num.toString().split(/[eE]/);
    var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
    return len > 0 ? len : 0;

/**
 * 把小数转成整数,支持科学计数法。如果是小数则放大成整数
 * @param *number num 输入数
 */
function float2Fixed(num) 
    if (num.toString().indexOf('e') === -1) 
        return Number(num.toString().replace('.', ''));
    
    var dLen = digitLength(num);
    return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num;

/**
 * 检测数字是否越界,如果越界给出提示
 * @param *number num 输入数
 */
function checkBoundary(num) 
    if (_boundaryCheckingState) 
        if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) 
            console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
        
    

/**
 * 精确乘法
 */
function times(num1, num2) 
    var others = [];
    for (var _i = 2; _i < arguments.length; _i++) 
        others[_i - 2] = arguments[_i];
    
    if (others.length > 0) 
        return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
    
    var num1Changed = float2Fixed(num1);
    var num2Changed = float2Fixed(num2);
    var baseNum = digitLength(num1) + digitLength(num2);
    var leftValue = num1Changed * num2Changed;
    checkBoundary(leftValue);
    return leftValue / Math.pow(10, baseNum);

/**
 * 精确加法
 */
function plus(num1, num2) 
    var others = [];
    for (var _i = 2; _i < arguments.length; _i++) 
        others[_i - 2] = arguments[_i];
    
    if (others.length > 0) 
        return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1)));
    
    var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
    return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;

/**
 * 精确减法
 */
function minus(num1, num2) 
    var others = [];
    for (var _i = 2; _i < arguments.length; _i++) 
        others[_i - 2] = arguments[_i];
    
    if (others.length > 0) 
        return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1)));
    
    var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
    return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;

/**
 * 精确除法
 */
function divide(num1, num2) 
    var others = [];
    for (var _i = 2; _i < arguments.length; _i++) 
        others[_i - 2] = arguments[_i];
    
    if (others.length > 0) 
        return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1)));
    
    var num1Changed = float2Fixed(num1);
    var num2Changed = float2Fixed(num2);
    checkBoundary(num1Changed);
    checkBoundary(num2Changed);
    // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
    return times((num1Changed / num2Changed), strip(Math.pow(10, digitLength(num2) - digitLength(num1))));

/**
 * 四舍五入
 */
function round(num, ratio) 
    var base = Math.pow(10, ratio);
    return divide(Math.round(times(num, base)), base);

var _boundaryCheckingState = true;
/**
 * 是否进行边界检查,默认开启
 * @param flag 标记开关,true 为开启,false 为关闭,默认为 true
 */
function enableBoundaryChecking(flag) 
    if (flag === void 0)  flag = true; 
    _boundaryCheckingState = flag;

var index =  strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking ;

exports.strip = strip;
exports.plus = plus;
exports.minus = minus;
exports.times = times;
exports.divide = divide;
exports.round = round;
exports.digitLength = digitLength;
exports.float2Fixed = float2Fixed;
exports.enableBoundaryChecking = enableBoundaryChecking;
exports['default'] = index;

以上是关于JavaScript的常用工具方法大全持续更新中...的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript的常用工具方法大全持续更新中...

PyQt5常用控件大全(详尽方法 + 实例操作,持续更新中…)

linux常用指令大全(持续更新中)

正则表达式大全(持续更新)|| 正则查找中文 || 正则查找注释

JavaScript中开发常用方法-总结-持续更新

docker常用命令大全(持续更新)