开发过程中常用的工具函数总结,提高开发效率,避免代码冗余
引用链接:https://juejin.im/post/5a2a7a5051882535cd4abfce#heading-53]
设备识别
browserInfo: function (type) {
switch (type) {
case 'android':
return navigator.userAgent.toLowerCase().indexOf('android') !== -1
case 'iphone':
return navigator.userAgent.toLowerCase().indexOf('iphone') !== -1
case 'ipad':
return navigator.userAgent.toLowerCase().indexOf('ipad') !== -1
case 'weixin':
return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1
default:
return navigator.userAgent.toLowerCase()
}
}
倒计时
//到某一个时间的倒计时
//ecDo.getEndTime('2017/7/22 16:0:0')
//result:"剩余时间6天 2小时 28 分钟20 秒"
getEndTime: function (endTime) {
var startDate = new Date(); //开始时间,当前时间
var endDate = new Date(endTime); //结束时间,需传入时间参数
var t = endDate.getTime() - startDate.getTime(); //时间差的毫秒数
var d = 0,
h = 0,
m = 0,
s = 0;
if (t >= 0) {
d = Math.floor(t / 1000 / 3600 / 24);
h = Math.floor(t / 1000 / 60 / 60 % 24);
m = Math.floor(t / 1000 / 60 % 60);
s = Math.floor(t / 1000 % 60);
}
return "剩余时间" + d + "天 " + h + "小时 " + m + " 分钟" + s + " 秒";
}
随机产生颜色
randomColor: function () {
return '#' + Math.random().toString(16).substring(2).substr(0, 6);
return color;
}
Math.floor(Math.random()*0xffffff).toString(16);
现金转大写
//ecDo.upDigit(168752632)
//result:"人民币壹亿陆仟捌佰柒拾伍万贰仟陆佰叁拾贰元整"
//ecDo.upDigit(1682)
//result:"人民币壹仟陆佰捌拾贰元整"
//ecDo.upDigit(-1693)
//result:"欠人民币壹仟陆佰玖拾叁元整"
upDigit: function (n) {
var fraction = ['角', '分', '厘'];
var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
var unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟']
];
var head = n < 0 ? '欠人民币' : '人民币';
n = Math.abs(n);
var s = '';
for (var i = 0; i < fraction.length; i++) {
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
}
s = s || '整';
n = Math.floor(n);
for (var i = 0; i < unit[0].length && n > 0; i++) {
var p = '';
for (var j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
//s = p + unit[0][i] + s;
}
return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}
返回数组(字符串)出现最多的几次元素和出现次数
getCount:function(str){
var str;
var json = {};
for (var i = 0; i < str.length; i++) {
if(!json[str.charAt(i) 大专栏 开发常用的工具函数]){
json[str.charAt(i)] = 1;
}else{
json[str.charAt(i)]++;
}
};
var iMax = 0;
var iIndex = '';
for(var i in json){
if(json[i]>iMax){
iMax = json[i];
iIndex = i;
}
}
}
数组的最大值和最小值
//数组最大值
maxArr: function (arr) {
return Math.max.apply(null, arr);
},
//数组最小值
minArr: function (arr) {
return Math.min.apply(null, arr);
}
数组去重
removeRepeatArray: function (arr) {
return arr.filter(function (item, index, self) {
return self.indexOf(item) === index;
});
}
检测密码强度
//ecDo.checkPwd('12asdASAD')
//result:3(强度等级为3)
checkPwd: function (str) {
var nowLv = 0;
if (str.length < 6) {
return nowLv
}
if (/[0-9]/.test(str)) {
nowLv++
}
if (/[a-z]/.test(str)) {
nowLv++
}
if (/[A-Z]/.test(str)) {
nowLv++
}
if (/[.|-|_]/.test(str)) {
nowLv++
}
return nowLv;
}
替换*
//字符替换*
//replaceStr(字符串,字符格式, 替换方式,替换的字符(默认*))
//ecDo.replaceStr('18819322663',[3,5,3],0)
//result:188*****663
//ecDo.replaceStr('asdasdasdaa',[3,5,3],1)
//result:***asdas***
//ecDo.replaceStr('1asd88465asdwqe3',[5],0)
//result:*****8465asdwqe3
//ecDo.replaceStr('1asd88465asdwqe3',[5],1,'+')
//result:"1asd88465as+++++"
replaceStr: function (str, regArr, type, ARepText) {
var regtext = '',
Reg = null,
replaceText = ARepText || '*';
//repeatStr是在上面定义过的(字符串循环复制),大家注意哦
if (regArr.length === 3 && type === 0) {
regtext = '(w{' + regArr[0] + '})w{' + regArr[1] + '}(w{' + regArr[2] + '})'
Reg = new RegExp(regtext);
var replaceCount = this.repeatStr(replaceText, regArr[1]);
return str.replace(Reg, '$1' + replaceCount + '$2')
}
else if (regArr.length === 3 && type === 1) {
regtext = 'w{' + regArr[0] + '}(w{' + regArr[1] + '})w{' + regArr[2] + '}'
Reg = new RegExp(regtext);
var replaceCount1 = this.repeatStr(replaceText, regArr[0]);
var replaceCount2 = this.repeatStr(replaceText, regArr[2]);
return str.replace(Reg, replaceCount1 + '$1' + replaceCount2)
}
else if (regArr.length === 1 && type === 0) {
regtext = '(^w{' + regArr[0] + '})'
Reg = new RegExp(regtext);
var replaceCount = this.repeatStr(replaceText, regArr[0]);
return str.replace(Reg, replaceCount)
}
else if (regArr.length === 1 && type === 1) {
regtext = '(w{' + regArr[0] + '}$)'
Reg = new RegExp(regtext);
var replaceCount = this.repeatStr(replaceText, regArr[0]);
return str.replace(Reg, replaceCount)
}
}
去除字符串空格
//去除空格 type 1-所有空格 2-前后空格 3-前空格 4-后空格
//ecDo.trim(' 1235asd',1)
//result:1235asd
//这个方法有原生的方案代替,但是考虑到有时候开发PC站需要兼容IE8,所以就还是继续保留
trim: function (str, type) {
switch (type) {
case 1:
return str.replace(/s+/g, "");
case 2:
return str.replace(/(^s*)|(s*$)/g, "");
case 3:
return str.replace(/(^s*)/g, "");
case 4:
return str.replace(/(s*$)/g, "");
default:
return str;
}
}