exportconstrandomNum=(min, max)=> Math.floor(Math.random()*(max - min +1))+ min;
数字千分位分隔
exportconstnumber_format(number, decimals, dec_point, thousands_sep){/*
* 参数说明:
* number:要格式化的数字
* decimals:保留几位小数
* dec_point:小数点符号
* thousands_sep:千分位符号
* */
number =(number +'').replace(/[^0-9+-Ee.]/g,'');var n =!isFinite(+number)?0:+number,
prec =!isFinite(+decimals)?0: Math.abs(decimals),
sep =(typeof thousands_sep ==='undefined')?',': thousands_sep,
dec =(typeof dec_point ==='undefined')?'.': dec_point,
s ='',toFixedFix=function(n, prec){var k = Math.pow(10, prec);return''+ Math.ceil(n * k)/ k;};
s =(prec ?toFixedFix(n, prec):''+ Math.round(n)).split('.');var re =/(-?\d+)(\d{3})/;while(re.test(s[0])){
s[0]= s[0].replace(re,"$1"+ sep +"$2");}if((s[1]||'').length < prec){
s[1]= s[1]||'';
s[1]+=newArray(prec - s[1].length +1).join('0');}return s.join(dec);}var num=number_format(1234567.089,2,".",",");//1,234,567.09
console.log(num);
数组操作
数组乱序
exportconstarrScrambling=(arr)=>{for(let i =0; i < arr.length; i++){const randomIndex = Math.round(Math.random()*(arr.length -1- i))+ i;[arr[i], arr[randomIndex]]=[arr[randomIndex], arr[i]];}return arr;}
数组扁平化
exportconstflatten=(arr)=>{let result =[];for(let i =0; i < arr.length; i++){if(Array.isArray(arr[i])){
result = result.concat(flatten(arr[i]));}else{
result.push(arr[i]);}}return result;}
exportconstrandomString=(len)=>{let chars ='ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';let strLen = chars.length;let randomStr ='';for(let i =0; i < len; i++){
randomStr += chars.charAt(Math.floor(Math.random()* strLen));}return randomStr;};
生成随机颜色
exportconstrandomColor=()=>{let str ="#";let random =0;let aryNum =["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];for(let i =0; i <6; i++){
random =parseInt(Math.random()*16);
str += aryNum[random];}return str;}
exportconsttoCDB=(str)=>{let result ="";for(let i =0; i < str.length; i++){
code = str.charCodeAt(i);if(code >=65281&& code <=65374){
result += String.fromCharCode(str.charCodeAt(i)-65248);}elseif(code ==12288){
result += String.fromCharCode(str.charCodeAt(i)-12288+32);}else{
result += str.charAt(i);}}return result;}
半角转换为全角
exportconsttoDBC=(str)=>{let result ="";for(let i =0; i < str.length; i++){
code = str.charCodeAt(i);if(code >=33&& code <=126){
result += String.fromCharCode(str.charCodeAt(i)+65248);}elseif(code ==32){
result += String.fromCharCode(str.charCodeAt(i)+12288-32);}else{
result += str.charAt(i);}}return result;}
exportconstdigitUppercase=(n)=>{const fraction =['角','分'];const digit =['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'];const unit =[['元','万','亿'],['','拾','佰','仟']];
n = Math.abs(n);let s ='';for(let 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(let i =0; i < unit[0].length && n >0; i++){let p ='';for(let 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;}return s.replace(/(零.)*零元/,'元').replace(/(零.)+/g,'零').replace(/^整$/,'零元整');};
数字转化为中文数字
exportconstintToChinese=(value)=>{const str =String(value);const len = str.length-1;const idxs =['','十','百','千','万','十','百','千','亿','十','百','千','万','十','百','千','亿'];const num =['零','一','二','三','四','五','六','七','八','九'];return str.replace(/([1-9]|0+)/g,($, $1, idx, full)=>{let pos =0;if($1[0]!=='0'){
pos = len-idx;if(idx ==0&& $1[0]==1&& idxs[len-idx]=='十'){return idxs[len-idx];}return num[$1[0]]+ idxs[len-idx];}else{let left = len - idx;let right = len - idx + $1.length;if(Math.floor(right /4)- Math.floor(left /4)>0){
pos = left - left %4;}if( pos ){return idxs[pos]+ num[$1[0]];}elseif( idx + $1.length >= len ){return'';}else{return num[$1[0]]}}});}
操作存储
存储loalStorage
exportconstloalStorageSet=(key, value)=>{if(!key)return;if(typeof value !=='string'){
value =JSON.stringify(value);}
window.localStorage.setItem(key, value);};
exportconstsessionStorageSet=(key, value)=>{if(!key)return;if(typeof value !=='string'){
value =JSON.stringify(value);}
window.sessionStorage.setItem(key, value)};
exportconstbroswer=()=>{const ua = navigator.userAgent.toLowerCase();if(ua.match(/MicroMessenger/i)=="micromessenger"){return"weixin";}elseif(ua.match(/QQ/i)=="qq"){return"QQ";}returnfalse;}