exportconstrandomNum=(min, max)=> Math.floor(Math.random()*(max - min +1))+ min;
2.数字千分位分隔
exportconstformat=(n)=>{
let num = n.toString();let len = num.length;if(len <=3){
return num;}else{
let temp ='';let remainder = len %3;if(remainder >0){
// 不是3的整数倍return num.slice(0, remainder)+','+ num.slice(remainder, len).match(/\d{3}/g).join(',')+ temp;}else{
// 3的整数倍return num.slice(0, len).match(/\d{3}/g).join(',')+ temp;}}}
数组操作
1.数组乱序
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;}
2.数组扁平化
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;}
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;}
7.半角转换为全角
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;}
格式转化
1.数字转化为大写金额
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(/^整$/,'零元整');};
2.数字转化为中文数字
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]]}}});}
存储操作
1.存储 localStorage
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)};
exportconsttoFullScreen=()=>{
let element = document.body;if(element.requestFullscreen){
element.requestFullscreen()}elseif(element.mozRequestFullScreen){
element.mozRequestFullScreen()}elseif(element.msRequestFullscreen){
element.msRequestFullscreen()}elseif(element.webkitRequestFullscreen){
element.webkitRequestFullScreen()}}
exportconstnowTime=()=>{
const now =newDate();const year = now.getFullYear();const month = now.getMonth();const date = now.getDate()>=10? now.getDate():('0'+ now.getDate());const hour = now.getHours()>=10? now.getHours():('0'+ now.getHours());const miu = now.getMinutes()>=10? now.getMinutes():('0'+ now.getMinutes());const sec = now.getSeconds()>=10? now.getSeconds():('0'+ now.getSeconds());return+year +"年"+(month +1)+"月"+ date +"日 "+ hour +":"+ miu +":"+ sec;}
2.格式化时间
exportconstdateFormater=(formater, time)=>{
let date = time ?newDate(time):newDate(),Y= date.getFullYear()+'',M= date.getMonth()+1,D= date.getDate(),H= date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();return formater.replace(/YYYY|yyyy/g,Y).replace(/YY|yy/g,Y.substr(2,2)).replace(/MM/g,(M<10?'0':'')+M).replace(/DD/g,(D<10?'0':'')+D).replace(/HH|hh/g,(H<10?'0':'')+H).replace(/mm/g,(m<10?'0':'')+ m).replace(/ss/g,(s<10?'0':'')+ s)}// dateFormater('YYYY-MM-DD HH:mm:ss')// dateFormater('YYYYMMDDHHmmss')
JS
1.阻止冒泡
exportconststopPropagation=(e)=>{
e = e || window.event;if(e.stopPropagation){
// W3C阻止冒泡方法
e.stopPropagation();}else{
e.cancelBubble =true;// IE阻止冒泡方法 }}
2.函数防抖
exportconstdebounce=(fn, wait)=>{
let timer =null;returnfunction(){
let context =this,
args = arguments;if(timer){
clearTimeout(timer);
timer =null;}
timer =setTimeout(()=>{
fn.apply(context, args);}, wait);};}