DDR爱好者之家 Design By 杰米

本文实例讲述了JavaScript封装的常用工具类库bee.js。分享给大家供大家参考,具体如下:

bee.js下载地址:

github下载地址:https://github.com/shadowOfCode/bee.js

或点击此处本站下载

使用:

<!--area.js存放区域编码的一个常量。由于bee.js里面的getPersonInfo18()方法需要调用这个常量,所以在bee.js之前引入。如果不需要用到这个方法也可以不引入area.js-->
<script type="text/javascript" src="/UploadFiles/2021-04-02/area.js">

该javaScript库主要包括了如下模块:

1、手机号码校验;

//电话号码
isPhoneCallNum: function(input)
//电信手机号码
isChinaTelecomPhoneNum: function(input)
//中国联通
isChinaUnicomPhoneNum: function(input)
//中国移动
isChinaMobilePhoneNum: function(input)
//手机号码
isPhoneNum: function(input)
//手机号码简单校验,只校验长度 
isPhoneNumBySize: function(input)

2、身份证校验;

//18位身份证简单校验
isSimpleIdCard18: function(idCard)
//15位身份证简单校验
isSimpleIdCard15: function(idCard)
//18位身份证校验码校验
checkCode: function(idCard)
//18位身份证严格校验
isIdCard18: function(idCard)
//根据18身份证号码获取人员信息 
getPersonInfo18:function(idCard)

//Demo
Bee.IdCardUtils.getPersonInfo18('350624199506094038');
//结果
{
  address: "福建省 漳州市 诏安县",
  sex: "男",
  birthday: "1995年06月09日",
  age: 23
}

3、邮箱校验;

//邮箱校验 
isEmail: function(input)

4、字符串常用类;

//空字符串
isEmpty: function(input)
//不是空字符串
isNotEmpty: function(input)
//空字符串,可为空格
isBlank: function(input)
//不是空字符串,空格也算空字符串
isNotBlank: function(input)
//去掉字符串两边的空格
trim: function(input)
//若为null则转为”
trimToEmpty: function(input)
//以某个字符串开头
startsWith: function(input, prefix)
//以某个字符串结尾
endsWith: function(input, suffix)
//包含某个子串
contains: function(input, searchSeq)
//判断字符串是否相等
equals: function(input1, input2)
//判断字符串是否相等,不区分大小写
equalsIgnoreCase: function(input1, input2)
//是否包含空白字符
containsWhitespace: function(input)
//生成指定个数的字符
repeat: function(ch, repeatTimes)
//删除空白字符
deleteWhitespace: function(input)
//右侧补全
ightPad: function(input, size, padStr)
//左侧补全
leftPad: function(input, size, padStr)
//首小写字母转大写
capitalize: function(input)
//首大写字母转小写
uncapitalize: function(input)
//大写转小写,小写转大写
swapCase: function(input)
//统计含有的子字符串的个数
countMatches: function(input, sub)
//只包含字母
isAlpha: function(input)
//只包含字母、空格
isAlphaSpace: function(input)
//只包含字母、数字
isAlphanumeric: function(input)
//只包含字母、数字和空格 
isAlphanumericSpace: function(input)
//数字
isNumeric: function(input)
//小数
isDecimal: function(input)
//负小数
isNegativeDecimal: function(input)
//正小数
isPositiveDecimal: function(input)
//整数
isInteger: function(input)
//正整数
isPositiveInteger: function(input)
//负整数 
isNegativeInteger: function(input)
//只包含数字和空格
isNumericSpace: function(input)
//是否为空白字符
sWhitespace: function(input)
//是否全为小写字母
isAllLowerCase: function(input)
//是否全为大写字母
sAllUpperCase: function(input)
//字符串为空时,默认值
defaultString: function(input, defaultStr)
//字符串为空时,默认值
defaultIfBlank: function(input, defaultStr)
//字符串为空时,默认值
defaultIfEmpty: function(input, defaultStr)
//字符串反转
reverse: function(input)
//删掉特殊字符(英文状态下)
removeSpecialCharacter: function(input)
//只包含特殊字符、数字和字母(不包括空格,若想包括空格,改为[ -~])
isSpecialCharacterAlphanumeric: function(input)
/** 
* @param {String} message 
* @param {Array} arr 
* 消息格式化 
*/ 
format: function(message, arr)
//demo
var message='我是{0}开发{1}';
var arr=['java','工程师'];
Bee.StringUtils.format(message,arr);

//结果
我是java开发工程师

/** 
* 把连续出现多次的字母字符串进行压缩。如输入:aaabbbbcccccd 输出:3a4b5cd 
* @param {String} input 
* @param {Boolean} ignoreCase : true or false 
*/ 
compressRepeatedStr: function(input, ignoreCase)
//中文校验
isChinese: function(input)
//去掉中文字符
removeChinese: function(input)
//转义元字符
escapeMetacharacter: function(input)
//转义字符串中的元字符
escapeMetacharacterOfStr: function(input)
//中文转为unicode编码 
chineseToUnicode: function(input)

5、简单四则运算;

//加法 
add: function(operandLeft, operandRight)
//减法
subtract: function(operandLeft, operandRight)
//乘法
multiply: function(operandLeft, operandRight)
//除法
divide: function(operandLeft, operandRight)
//校验表达式的合法性
isArithmeticExpression: function(expression)
//计算
calculate: function(expression)
//中缀表达式转后缀表达式
infixToPostfixExpression: function(expression)

//demo
var expression='(2+9)*8-24';
Bee.ElementaryArithmeticUtils.infixToPostfixExpression(expression);

//结果
2 9 + 8 * 24 -

//中缀表达式转前缀表达式(结果以空格隔开) 
infixToPrefixExpression: function(expression)

//demo
var expression='(2+9)*8-24';
Bee.ElementaryArithmeticUtils.infixToPrefixExpression(expression);

//结果
- * + 2 9 8 24

//解决正负号问题-1转为0-1;+1转为0+1
eliminatePositiveOrNegativeSign: function(expression)
//把中缀表达式转为前缀表达式,再计算
calculateByPrefixExpression: function(expression)
//demo
var expression='(2+9)*8-24';
Bee.ElementaryArithmeticUtils.calculateByPrefixExpression(expression);

//结果
["64"]

//把中缀表达式转为后缀表达式,再计算 
calculateByPostfixExpression: function(expression)
//demo
var expression='(2+9)*8-24';
Bee.ElementaryArithmeticUtils.calculateByPostfixExpression(expression);

//结果
["64"]

//横式计算 
horizontalCalculation: function(expression)
var expression='1+2*(4-3)/5*[(7-6)/8*9]';
Bee.ElementaryArithmeticUtils.horizontalCalculation(expression);

//结果
1+2*(4-3)/5*[(7-6)/8*9]=1+2*1/5*[1/8*9]=1+2*1/5*1.125=1+2/5*1.125=1+0.4*1.125=1+0.45=1.45

//竖式计算 
verticalCalculation: function(expression)
var expression='1+2*(4-3)/5*[(7-6)/8*9]';
Bee.ElementaryArithmeticUtils.verticalCalculation(expression);

1+2*(4-3)/5*[(7-6)/8*9]
=1+2*1/5*[1/8*9]
=1+2*1/5*1.125
=1+2/5*1.125
=1+0.4*1.125
=1+0.45
=1.45

6、正则表达式生成工具类;

[ ] //生成正整数范围的表达式
positiveIntegerRange:function(minimum,maximum)

排除某些字符串,即不能包含某些字符串.返回值为RegExp对象

createRegexObjMustExclude:function(input, conditions)

参数说明

@param {Object} conditions:里面有多个属性,如下:
@param {String} matcherFlag 匹配标识
0:数字;
1:字母;
2:小写字母;
3:大写字母;
4:特殊字符,指英文状态下的标点符号及括号等;
5:中文;
6:数字和字母;
7:数字和小写字母;
8:数字和大写字母;
9:数字、字母和特殊字符;
10:数字和中文;
11:小写字母和特殊字符;
12:大写字母和特殊字符;
13:字母和特殊字符;
14:小写字母和中文;
15:大写字母和中文;
16:字母和中文;
17:特殊字符、和中文;
18:特殊字符、字母和中文;
19:特殊字符、小写字母和中文;
20:特殊字符、大写字母和中文;
100:所有字符;

@param {Array} targetStrArr 排除的字符串,数组格式
@param {String} length 长度,可为空。1,2表示长度1到2之间;10,表示10个以上字符;5表示长度为5
@param {Boolean} ignoreCase 是否忽略大小写

条件参数固定格式

 

conditions={matcherFlag:”0”,targetStrArr:[],length:”“,ignoreCase:true}
var conditions={matcherFlag:"0",targetStrArr:['12','00'],length:"10",ignoreCase:true}
Bee.RegexUtils.createRegexObjMustExclude("1234567009",conditions);
//生成的正则表达式
/^("0",targetStrArr:['12','00'],length:"10",ignoreCase:true}
Bee.RegexUtils.isPatternMustExclude("1234567009",conditions);

//结果
false

//demo2,10位长度的数字,且不能包含120子串
var conditions={matcherFlag:"0",targetStrArr:['120'],length:"10",ignoreCase:true}
Bee.RegexUtils.isPatternMustExclude("1234567009",conditions);

//结果
true

必须同时包含某些字符串,返回值为RegExp对象

createRegexObjMustContain:function()
var conditions={matcherFlag:"0",targetStrArr:['120'],length:"10",ignoreCase:true}
Bee.RegexUtils.createRegexObjMustContain("1234567009",conditions);

/^("htmlcode">
isPatternMustContain: function(input, conditions)
//demo1
var conditions={matcherFlag:"0",targetStrArr:['120'],length:"10",ignoreCase:true}
Bee.RegexUtils.isPatternMustContain("1234567009",conditions);

false

//demo2
var conditions={matcherFlag:"0",targetStrArr:['12','00'],length:"10",ignoreCase:true}
Bee.RegexUtils.isPatternMustContain("1234567009",conditions);

true

更多关于JavaScript相关内容还可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

福利放送 2024/3/29

跑跑卡丁车单机版 2020-11-1(可与AI一起玩) 天翼云/百度云