1.将阿拉伯数字翻译成中文的大写数字
export const numberToChinese = ( num ) => {
let AA = new Array (
"零" ,
"一" ,
"二" ,
"三" ,
"四" ,
"五" ,
"六" ,
"七" ,
"八" ,
"九" ,
"十"
) ;
let BB = new Array ( "" , "十" , "百" , "仟" , "萬" , "億" , "点" , "" ) ;
let a = ( "" + num) . replace ( / (^0*) / g , "" ) . split ( "." ) ,
k = 0 ,
re = "" ;
for ( let i = a[ 0 ] . length - 1 ; i >= 0 ; i-- ) {
switch ( k) {
case 0 :
re = BB [ 7 ] + re;
break ;
case 4 :
if ( ! new RegExp ( "0{4}//d{" + ( a[ 0 ] . length - i - 1 ) + "}$" ) . test ( a[ 0 ] ) )
re = BB [ 4 ] + re;
break ;
case 8 :
re = BB [ 5 ] + re;
BB [ 7 ] = BB [ 5 ] ;
k = 0 ;
break ;
}
if ( k % 4 == 2 && a[ 0 ] . charAt ( i + 2 ) != 0 && a[ 0 ] . charAt ( i + 1 ) == 0 )
re = AA [ 0 ] + re;
if ( a[ 0 ] . charAt ( i) != 0 ) re = AA [ a[ 0 ] . charAt ( i) ] + BB [ k % 4 ] + re;
k++ ;
}
if ( a. length > 1 ) {
re += BB [ 6 ] ;
for ( let i = 0 ; i < a[ 1 ] . length; i++ ) re += AA [ a[ 1 ] . charAt ( i) ] ;
}
if ( re == "一十" ) re = "十" ;
if ( re. match ( / ^一 / ) && re. length == 3 ) re = re. replace ( "一" , "" ) ;
return re;
} ;
2.将数字转换为大写金额
export const changeToChinese = ( Num ) => {
if ( typeof Num == "number" ) {
Num = new String ( Num) ;
}
Num = Num. replace ( / , / g , "" ) ;
Num = Num. replace ( / / g , "" ) ;
Num = Num. replace ( / ¥ / g , "" ) ;
if ( isNaN ( Num) ) {
return "" ;
}
let part = String ( Num) . split ( "." ) ;
let newchar = "" ;
for ( let i = part[ 0 ] . length - 1 ; i >= 0 ; i-- ) {
if ( part[ 0 ] . length > 10 ) {
return "" ;
}
let tmpnewchar = "" ;
let perchar = part[ 0 ] . charAt ( i) ;
switch ( perchar) {
case "0" :
tmpnewchar = "零" + tmpnewchar;
break ;
case "1" :
tmpnewchar = "壹" + tmpnewchar;
break ;
case "2" :
tmpnewchar = "贰" + tmpnewchar;
break ;
case "3" :
tmpnewchar = "叁" + tmpnewchar;
break ;
case "4" :
tmpnewchar = "肆" + tmpnewchar;
break ;
case "5" :
tmpnewchar = "伍" + tmpnewchar;
break ;
case "6" :
tmpnewchar = "陆" + tmpnewchar;
break ;
case "7" :
tmpnewchar = "柒" + tmpnewchar;
break ;
case "8" :
tmpnewchar = "捌" + tmpnewchar;
break ;
case "9" :
tmpnewchar = "玖" + tmpnewchar;
break ;
}
switch ( part[ 0 ] . length - i - 1 ) {
case 0 :
tmpnewchar = tmpnewchar + "元" ;
break ;
case 1 :
if ( perchar != 0 ) tmpnewchar = tmpnewchar + "拾" ;
break ;
case 2 :
if ( perchar != 0 ) tmpnewchar = tmpnewchar + "佰" ;
break ;
case 3 :
if ( perchar != 0 ) tmpnewchar = tmpnewchar + "仟" ;
break ;
case 4 :
tmpnewchar = tmpnewchar + "万" ;
break ;
case 5 :
if ( perchar != 0 ) tmpnewchar = tmpnewchar + "拾" ;
break ;
case 6 :
if ( perchar != 0 ) tmpnewchar = tmpnewchar + "佰" ;
break ;
case 7 :
if ( perchar != 0 ) tmpnewchar = tmpnewchar + "仟" ;
break ;
case 8 :
tmpnewchar = tmpnewchar + "亿" ;
break ;
case 9 :
tmpnewchar = tmpnewchar + "拾" ;
break ;
}
let newchar = tmpnewchar + newchar;
}
if ( Num. indexOf ( "." ) != - 1 ) {
if ( part[ 1 ] . length > 2 ) {
part[ 1 ] = part[ 1 ] . substr ( 0 , 2 ) ;
}
for ( i = 0 ; i < part[ 1 ] . length; i++ ) {
tmpnewchar = "" ;
perchar = part[ 1 ] . charAt ( i) ;
switch ( perchar) {
case "0" :
tmpnewchar = "零" + tmpnewchar;
break ;
case "1" :
tmpnewchar = "壹" + tmpnewchar;
break ;
case "2" :
tmpnewchar = "贰" + tmpnewchar;
break ;
case "3" :
tmpnewchar = "叁" + tmpnewchar;
break ;
case "4" :
tmpnewchar = "肆" + tmpnewchar;
break ;
case "5" :
tmpnewchar = "伍" + tmpnewchar;
break ;
case "6" :
tmpnewchar = "陆" + tmpnewchar;
break ;
case "7" :
tmpnewchar = "柒" + tmpnewchar;
break ;
case "8" :
tmpnewchar = "捌" + tmpnewchar;
break ;
case "9" :
tmpnewchar = "玖" + tmpnewchar;
break ;
}
if ( i == 0 ) tmpnewchar = tmpnewchar + "角" ;
if ( i == 1 ) tmpnewchar = tmpnewchar + "分" ;
newchar = newchar + tmpnewchar;
}
}
while ( newchar. search ( "零零" ) != - 1 ) newchar = newchar. replace ( "零零" , "零" ) ;
newchar = newchar. replace ( "零亿" , "亿" ) ;
newchar = newchar. replace ( "亿万" , "亿" ) ;
newchar = newchar. replace ( "零万" , "万" ) ;
newchar = newchar. replace ( "零元" , "元" ) ;
newchar = newchar. replace ( "零角" , "" ) ;
newchar = newchar. replace ( "零分" , "" ) ;
if ( newchar. charAt ( newchar. length - 1 ) == "元" ) {
newchar = newchar + "整" ;
}
return newchar;
} ;
3. 判断一个元素是否在数组中和数组排序
export const contains = ( arr, val ) => {
return arr. indexOf ( val) != - 1 ? true : false ;
} ;
export const sort = ( arr, type = 1 ) => {
return arr. sort ( ( a, b ) => {
switch ( type) {
case 1 :
return a - b;
case 2 :
return b - a;
case 3 :
return Math. random ( ) - 0.5 ;
default :
return arr;
}
} ) ;
} ;
4.数组去重
export const unique = ( arr ) => {
if ( Array. hasOwnProperty ( "from" ) ) {
return Array. from ( new Set ( arr) ) ;
} else {
let n = { } ,
r = [ ] ;
for ( let i = 0 ; i < arr. length; i++ ) {
if ( ! n[ arr[ i] ] ) {
n[ arr[ i] ] = true ;
r. push ( arr[ i] ) ;
}
}
return r;
}
} ;
5.求两个集合的并集和求两个集合的交集
export const union = ( a, b ) => {
let newArr = a. concat ( b) ;
return this . unique ( newArr) ;
} ;
export const intersect = ( a, b ) => {
let _this = this ;
a = this . unique ( a) ;
return this . map ( a, function ( o ) {
return _this. contains ( b, o) ? o : null ;
} ) ;
} ;
6.删除其中一个元素、将类数组转换为数组、最大值、最小值、求和、平均值
export const remove = ( arr, ele ) => {
let index = arr. indexOf ( ele) ;
if ( index > - 1 ) {
arr. splice ( index, 1 ) ;
}
return arr;
} ;
export const formArray = ( ary ) => {
let arr = [ ] ;
if ( Array. isArray ( ary) ) {
arr = ary;
} else {
arr = Array . prototype. slice . call ( ary) ;
}
return arr;
} ;
export const max = ( arr ) => {
return Math. max . apply ( null , arr) ;
} ;
export const min = ( arr ) => {
return Math. min . apply ( null , arr) ;
} ;
export const sum = ( arr ) => {
return arr. reduce ( ( pre, cur ) => {
return pre + cur;
} ) ;
} ;
export const average = ( arr ) => {
return this . sum ( arr) / arr. length;
} ;
7.去除空格
export const trim = ( str, type ) => {
type = type || 1 ;
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;
}
} ;
8.字符替换
export const changeCase = ( str, type ) => {
type = type || 4 ;
switch ( type) {
case 1 :
return str. replace ( / \b\w+\b / g , function ( word ) {
return (
word. substring ( 0 , 1 ) . toUpperCase ( ) + word. substring ( 1 ) . toLowerCase ( )
) ;
} ) ;
case 2 :
return str. replace ( / \b\w+\b / g , function ( word ) {
return (
word. substring ( 0 , 1 ) . toLowerCase ( ) + word. substring ( 1 ) . toUpperCase ( )
) ;
} ) ;
case 3 :
return str
. split ( "" )
. map ( function ( word ) {
if ( / [a-z] / . test ( word) ) {
return word. toUpperCase ( ) ;
} else {
return word. toLowerCase ( ) ;
}
} )
. join ( "" ) ;
case 4 :
return str. toUpperCase ( ) ;
case 5 :
return str. toLowerCase ( ) ;
default :
return str;
}
} ;
9.检测密码强度
export const checkPwd = ( str ) => {
let Lv = 0 ;
if ( str. length < 6 ) {
return Lv;
}
if ( / [0-9] / . test ( str) ) {
Lv++ ;
}
if ( / [a-z] / . test ( str) ) {
Lv++ ;
}
if ( / [A-Z] / . test ( str) ) {
Lv++ ;
}
if ( / [\.|-|_] / . test ( str) ) {
Lv++ ;
}
return Lv;
} ;
10. 函数节流器
export const debouncer = ( fn, time, interval = 200 ) => {
if ( time - ( window. debounceTimestamp || 0 ) > interval) {
fn && fn ( ) ;
window. debounceTimestamp = time;
}
} ;
11.在字符串中插入新字符串
export const insertStr = ( soure, index, newStr ) => {
let str = soure. slice ( 0 , index) + newStr + soure. slice ( index) ;
return str;
} ;
12.判断两个对象是否键值相同
export const isObjectEqual = ( a, b ) => {
let aProps = Object. getOwnPropertyNames ( a) ;
let bProps = Object. getOwnPropertyNames ( b) ;
if ( aProps. length !== bProps. length) {
return false ;
}
for ( let i = 0 ; i < aProps. length; i++ ) {
let propName = aProps[ i] ;
if ( a[ propName] !== b[ propName] ) {
return false ;
}
}
return true ;
} ;
13.16进制颜色转RGBRGBA字符串
export const colorToRGB = ( val, opa ) => {
let pattern = / ^(#?)[a-fA-F0-9]{6}$ / ;
let isOpa = typeof opa == "number" ;
if ( ! pattern. test ( val) ) {
return "" ;
}
let v = val. replace ( / # / , "" ) ;
let rgbArr = [ ] ;
let rgbStr = "" ;
for ( let i = 0 ; i < 3 ; i++ ) {
let item = v. substring ( i * 2 , i * 2 + 2 ) ;
let num = parseInt ( item, 16 ) ;
rgbArr. push ( num) ;
}
rgbStr = rgbArr. join ( ) ;
rgbStr =
"rgb" + ( isOpa ? "a" : "" ) + "(" + rgbStr + ( isOpa ? "," + opa : "" ) + ")" ;
return rgbStr;
} ;
14.追加url参数
export const appendQuery = ( url, key, value ) => {
let options = key;
if ( typeof options == "string" ) {
options = { } ;
options[ key] = value;
}
options = $. param ( options) ;
if ( url. includes ( "?" ) ) {
url += "&" + options;
} else {
url += "?" + options;
}
return url;
} ;