VUE过滤器,时间,字符串,空格,⾦额⼤写转换函数关于时间,空格,格式化处理字符串,现⾦额⼤写转换函数,保留2位⼩数
function trim(value, trim){
switch(trim){
case1:
place(/\s+/g,"");
case2:
place(/(^\s*)|(\s*$)/g,"");
case3:
place(/(^\s*)/g,"");
case4:
place(/(\s*$)/g,"");
default:
return value;
}
}
//任意格式⽇期处理
//使⽤格式:
// {{ '2018-09-14 01:05' | formaDate(yyyy-MM-dd hh:mm:ss) }}
// {{ '2018-09-14 01:05' | formaDate(yyyy-MM-dd) }}
// {{ '2018-09-14 01:05' | formaDate(MM/dd) }} 等
function formaDate(value, fmt){
var date =new Date(value);
var o ={
"M+": Month()+1,//⽉份
"d+": Date(),//⽇
"h+": Hours(),//⼩时
"m+": Minutes(),//分
"s+": Seconds(),//秒
"w+": Day(),//星期
"q+": Math.floor((Month()+3)/3),//季度
"S": Milliseconds()//毫秒
};
if(/(y+)/.test(fmt)) fmt = place(RegExp.$1,(FullYear()+"").substr(4- RegExp.$1.length));
for(var k in o){
if(k ==='w+'){
if(o[k]===0){
fmt = place('w','周⽇');
}else if(o[k]===1){
fmt = place('w','周⼀');
}else if(o[k]===2){
fmt = place('w','周⼆');
}else if(o[k]===3){
fmt = place('w','周三');
}else if(o[k]===4){
fmt = place('w','周四');
}else if(o[k]===5){
fmt = place('w','周五');
}else if(o[k]===6){
fmt = place('w','周六');
}
}else if(new RegExp("("+ k +")").test(fmt)){
fmt = place(RegExp.$1,(RegExp.$1.length ==1)?(o[k]):(("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
//字母⼤⼩写切换
/*type
1:⾸字母⼤写
2:⾸页母⼩写
3:⼤⼩写转换
4:全部⼤写
5:全部⼩写
5:全部⼩写
* */
function changeCase(str, type){
function ToggleCase(str){
var itemText =""
str.split("").forEach(
function(item){
if(/^([a-z]+)/.test(item)){
itemText += UpperCase();
}else if(/^([A-Z]+)/.test(item)){
itemText += LowerCase();
}else{
itemText += item;
}
});
return itemText;
}
switch(type){replaceall()
case1:
place(/\b\w+\b/g,function(word){
return word.substring(0,1).toUpperCase()+ word.substring(1).toLowerCase();
});
case2:
place(/\b\w+\b/g,function(word){
return word.substring(0,1).toLowerCase()+ word.substring(1).toUpperCase();
});
case3:
return ToggleCase(str);
case4:
UpperCase();
case5:
LowerCase();
default:
return str;
}
}
/
/字符串循环复制,count->次数
function repeatStr(str, count){
var text ='';
for(var i =0; i < count; i++){
text += str;
}
return text;
}
//字符串替换
function replaceAll(str, AFindText, ARepText){
raRegExp =new RegExp(AFindText,"g");
place(raRegExp, ARepText);
}
//字符替换*,隐藏⼿机号或者⾝份证号等
//replaceStr(字符串,字符格式, 替换⽅式,替换的字符(默认*))
//placeStr('188********',[3,5,3],0)
//result:188*****663
//placeStr('asdasdasdaa',[3,5,3],1)
//result:***asdas***
//placeStr('1asd88465asdwqe3',[5],0)
//result:*****8465asdwqe3
//placeStr('1asd88465asdwqe3',[5],1,'+')
/
/result:"1asd88465as+++++"
function replaceStr(str, regArr, type, ARepText){
var regtext ='',
Reg =null,
replaceText = ARepText ||'*';
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 =peatStr(replaceText, regArr[1]);
place(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 =peatStr(replaceText, regArr[0]);
var replaceCount2 =peatStr(replaceText, regArr[2]);
place(Reg, replaceCount1 +'$1'+ replaceCount2)
}
else if(regArr.length ===1&& type ===0){
regtext ='(^\\w{'+ regArr[0]+'})'
Reg =new RegExp(regtext);
var replaceCount =peatStr(replaceText, regArr[0]);
place(Reg, replaceCount)
}
else if(regArr.length ===1&& type ===1){
regtext ='(\\w{'+ regArr[0]+'}$)'
Reg =new RegExp(regtext);
var replaceCount =peatStr(replaceText, regArr[0]);
place(Reg, replaceCount)
}
}
//字符串循环复制,count->次数
function repeatStr(str, count){
var text ='';
for(var i =0; i < count; i++){
text += str;
}
return text;
}
//字符串替换
function replaceAll(str, AFindText, ARepText){
raRegExp =new RegExp(AFindText,"g");
place(raRegExp, ARepText);
}
/
/字符替换*,隐藏⼿机号或者⾝份证号等
//replaceStr(字符串,字符格式, 替换⽅式,替换的字符(默认*))
//placeStr('188********',[3,5,3],0)
//result:188*****663
//placeStr('asdasdasdaa',[3,5,3],1)
//result:***asdas***
//placeStr('1asd88465asdwqe3',[5],0)
//result:*****8465asdwqe3
//placeStr('1asd88465asdwqe3',[5],1,'+')
//result:"1asd88465as+++++"
function replaceStr(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 =peatStr(replaceText, regArr[1]);
place(Reg,'$1'+ replaceCount +'$2')
}
else if(regArr.length ===3&& type ===1){
else if(regArr.length ===3&& type ===1){
regtext ='\\w{'+ regArr[0]+'}(\\w{'+ regArr[1]+'})\\w{'+ regArr[2]+'}'
Reg =new RegExp(regtext);
var replaceCount1 =peatStr(replaceText, regArr[0]);
var replaceCount2 =peatStr(replaceText, regArr[2]);
place(Reg, replaceCount1 +'$1'+ replaceCount2)
}
else if(regArr.length ===1&& type ===0){
regtext ='(^\\w{'+ regArr[0]+'})'
Reg =new RegExp(regtext);
var replaceCount =peatStr(replaceText, regArr[0]);
place(Reg, replaceCount)
}
else if(regArr.length ===1&& type ===1){
regtext ='(\\w{'+ regArr[0]+'}$)'
Reg =new RegExp(regtext);
var replaceCount =peatStr(replaceText, regArr[0]);
place(Reg, replaceCount)
}
}
//格式化处理字符串
//ecDo.formatText('1234asda567asd890')
//result:"12,34a,sda,567,asd,890"
/
/ecDo.formatText('1234asda567asd890',4,' ')
//result:"1 234a sda5 67as d890"
//ecDo.formatText('1234asda567asd890',4,'-')
//result:"1-234a-sda5-67as-d890"
function formatText(str, size, delimiter){
var _size = size ||3, _delimiter = delimiter ||',';
var regText ='\\B(?=(\\w{'+ _size +'})+(?!\\w))';
var reg =new RegExp(regText,'g');
place(reg, _delimiter);
}
//现⾦额⼤写转换函数
/
/ecDo.upDigit(168752632)
//result:"⼈民币壹亿陆仟捌佰柒拾伍万贰仟陆佰叁拾贰元整"
//ecDo.upDigit(1682)
//result:"⼈民币壹仟陆佰捌拾贰元整"
//ecDo.upDigit(-1693)
//result:"⽋⼈民币壹仟陆佰玖拾叁元整"
function upDigit(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(/^整$/,'零元整'); }
//保留2位⼩数
function toDecimal2(x){
var f =parseFloat(x);
if(isNaN(f)){
return false;
}
var f = und(x *100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if(rs <0){
rs = s.length;
s +='.';
}
while(s.length <= rs +2){
s +='0';
}
return s;
}
export{
trim,
changeCase,
repeatStr,
replaceAll,
replaceStr,
checkPwd,
formatText,
upDigit,
toDecimal2,
formaDate
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。