文章目录 1. 任意区间的的随机数 2. 判断一个数是不是质数 3. 传入指定 key 返回当前页面 search 的 value,也可以手动传入url 4. 在 Date 上封装了一个返回指定格式的方法 5. 数组去重 6. 预览pdf,后端返回的文件流设置的只能下载,需要预览 7. 下载pdf,后端返回的文件流,需要下载 8. 为元素添加拖拽功能,通过 transform 改变的位置 9. 树结构转数组,通过id和pid关联 10. 通过 pid 和 id 关联的数组转树结构 11. 数组对象数据过滤,两个一维数组如果存在相同id则替换指定字段,如果不同则添加 12. 进度转百分比 13. 时间对象转天数 14. 天数转时间对象 15. 计算两个时间戳相差多少天 16. 获取当月天数 17. 随机生成 16 进制颜色 18. 控制异步并发数
1. 任意区间的的随机数
const getRandom = ( max, min ) => Math. floor ( Math. random ( ) * ( max - min + 1 ) + min) ;
2. 判断一个数是不是质数
function isPrime ( num ) { let temp = Math. floor ( Math. sqrt ( num) ) ; for ( let i = 2 ; i <= temp; i++ ) { if ( num % i == 0 ) { return false ; } } return true ;
}
3. 传入指定 key 返回当前页面 search 的 value,也可以手动传入url
function getSearch ( key, url = "" ) { let search = url. split ( "?" ) [ 1 ] || location. search. substring ( 1 ) ; let obj = { } ; search. split ( "&" ) . forEach ( ( item ) => { let temp = item. split ( "=" ) ; obj[ temp[ 0 ] ] = temp[ 1 ] ; } ) ; return obj[ key] ;
}
4. 在 Date 上封装了一个返回指定格式的方法
Date. format = function ( type = "-" , date) { date = date && new Date ( date) ; let time = date || new this ( ) ; let y = time. getFullYear ( ) ; let M = time. getMonth ( ) + 1 ; let d = time. getDate ( ) ; let h = time. getHours ( ) ; let m = time. getMinutes ( ) ; let s = time. getSeconds ( ) ; M = M > 9 ? M : "0" + M ; d = d > 9 ? d : "0" + d; h = h > 9 ? h : "0" + h; m = m > 9 ? m : "0" + m; s = s > 9 ? s : "0" + s; if ( type === 0 ) { return ` ${ y} 年 ${ M } 月 ${ d} 日 ${ h} 时 ${ m} 分 ${ s} 秒 ` ; } return ` ${ y} ${ type} ${ M } ${ type} ${ d} ${ h} ${ type} ${ m} ${ type} ${ s} ` ;
} ;
5. 数组去重
const toRepeat = ( arr ) => [ ... new Set ( arr) ] ;
6. 预览pdf,后端返回的文件流设置的只能下载,需要预览
function previewPdf ( url ) { $. ajax ( { cache : true , type : "GET" , url, async : false , mimeType : "text/plain; charset=x-user-defined" , success ( data ) { var rawLength = data. length; var array = new Uint8Array ( new ArrayBuffer ( rawLength) ) ; for ( let i = 0 ; i < rawLength; i++ ) { array[ i] = data. charCodeAt ( i) & 0xff ; } var blob = new Blob ( [ array] , { type : "application/pdf;charset-UTF-8" } ) ; var href = URL . createObjectURL ( blob) ; open ( href) ; URL . revokeObjectURL ( href) ; } , error ( ) { alert ( "网络原因请求失败!" ) ; } , } ) ;
}
7. 下载pdf,后端返回的文件流,需要下载
function downloadPdf ( url ) { $. ajax ( { type : "POST" , url, headers : { "Content-Type" : "application/json" , } , xhrFields : { responseType : "blob" } , data : JSON . stringify ( { projectId : null } ) , success : ( res ) => { const blob = new Blob ( [ res] , { type : "application/pdf" , } ) ; let a = document. createElement ( "a" ) ; a. download = "demo.pdf" ; a. href = URL . createObjectURL ( blob) ; a. click ( ) ; URL . revokeObjectURL ( href) ; } , error ( ) { alert ( "网络原因请求失败!" ) ; } , } ) ;
}
8. 为元素添加拖拽功能,通过 transform 改变的位置
function elDrag ( el ) { let initX = null ; let initY = null ; el. style. cursor = "move" ; el. addEventListener ( "mousedown" , function ( e ) { let curX = 0 ; let curY = 0 ; let str = el. style. transform. trim ( ) ; if ( str) { let reg = / \-?\d+ / g ; curX = Number ( reg. exec ( str) ) ; curY = Number ( reg. exec ( str) ) ; } initX = e. pageX - curX; initY = e. pageY - curY; document. addEventListener ( "mousemove" , moveEle) ; } ) ; function moveEle ( e ) { let x = e. pageX - initX; let y = e. pageY - initY; el. style. transform = ` translate( ${ x} px, ${ y} px) ` ; } document. addEventListener ( "mouseup" , function ( ) { document. removeEventListener ( "mousemove" , moveEle) ; } ) ;
}
9. 树结构转数组,通过id和pid关联
function treeToArr ( tree, child = "children" ) { let arr = JSON . parse ( JSON . stringify ( tree) ) ; while ( arr. some ( ( i ) => i. hasOwnProperty ( child) ) ) { arr. forEach ( ( item ) => { if ( ! item. hasOwnProperty ( "pid" ) ) item. pid = "-1" ; if ( item[ child] && item[ child] . length) { arr. push ( ... item[ child] . map ( ( i ) => ( { ... i, pid : item. id } ) ) ) ; } delete item[ child] ; } ) ; } return arr;
}
10. 通过 pid 和 id 关联的数组转树结构
function arrToTree ( arr, child = "children" ) { let tree = JSON . parse ( JSON . stringify ( arr) ) ; tree. forEach ( ( item ) => { item[ child] = tree. filter ( ( i ) => i. pid === item. id) ; tree = tree. filter ( ( i ) => i. pid !== item. id) ; } ) ; return tree;
}
function arrToTree2 ( arr, child = "children" , rootId = "-1" ) { let result = JSON . parse ( JSON . stringify ( arr) ) ; return result. reduce ( ( pre, cur, i, arr ) => { cur[ child] = arr. filter ( ( item ) => item. pid === cur. id) ; if ( cur. pid === rootId) { pre. push ( cur) ; } return pre; } , [ ] ) ;
}
11. 数组对象数据过滤,两个一维数组如果存在相同id则替换指定字段,如果不同则添加
function dateReplace ( target, source ) { let arr1 = JSON . parse ( JSON . stringify ( target) ) ; let arr2 = JSON . parse ( JSON . stringify ( source) ) ; arr1. forEach ( ( item ) => { arr2. find ( ( i ) => { if ( item. id === i. id) { for ( let k in i) { if ( k !== "id" ) { item[ k] = i[ k] ; } } i. replace = true ; } } ) ; } ) ; return [ ... arr1, ... arr2. filter ( ( item ) => ! item. replace) ] ;
}
12. 进度转百分比
const stepsToRatio = ( progress, type = 0 ) => { if ( progress === null ) progress = Number ( progress) ; if ( typeof progress !== "number" ) throw new Error ( "只能识别数字类型" ) ; if ( type === 1 ) { return progress === 0 ? "未开始" : "已完成" ; } return Math. floor ( progress * 100 ) + "%" ;
} ;
13. 时间对象转天数
const dateToDay = ( time ) => + new Date ( time) / ( 1000 * 3600 * 24 ) ;
14. 天数转时间对象
const dayToDate = ( day ) => new Date ( day * ( 1000 * 3600 * 24 ) ) ;
15. 计算两个时间戳相差多少天
const daysInterval = ( startTime, endTime ) => Math. abs ( Math. round ( ( + new Date ( endTime) - + new Date ( startTime) ) / ( 1000 * 3600 * 24 ) ) ) ;
16. 获取当月天数
function getDays ( ) { let date = new Date ( ) ; date. setMonth ( date. getMonth ( ) + 1 ) ; date. setDate ( 0 ) ; return date. getDate ( ) ;
}
17. 随机生成 16 进制颜色
function randomHexcolor ( ) { return "#" + Math. random ( ) . toString ( "16" ) . substring ( 2 , 8 ) ;
}
18. 控制异步并发数
class SuperTask { constructor ( concurrency = 2 ) { this . concurrency = concurrency; this . queueCount = 0 ; this . tasks = [ ] ; } addTask ( cb, time, resCb ) { this . _add ( ( ) => this . _timeout ( time) . then ( cb) , resCb) ; } _add ( task, resCb ) { this . tasks. push ( { task, resCb, } ) ; this . _run ( ) ; } async _run ( ) { while ( this . queueCount < this . concurrency && this . tasks. length) { let { task, resCb } = this . tasks. shift ( ) ; this . queueCount++ ; let res = await task ( ) ; resCb && resCb ( res) ; this . queueCount-- ; this . _run ( ) ; } } _timeout ( time ) { return new Promise ( ( resolve, reject ) => { setTimeout ( ( ) => { resolve ( ) ; } , time) ; } ) ; }
}