import { tasInterface , currencyInterface , explorerInfoModel } from './interface' ; export default class Tas { public throttle : tasInterface = ( func , awai = 200 ) => { let lastTime = 0 ; return ( ... args ) => { let now = new Date ( ) . getTime ( ) ; if ( now - lastTime > awai ) { func . apply ( this , args ) ; lastTime = now ; } } ; } ; public debounce : tasInterface = ( func , awai = 200 ) => { let timer = null ; return ( ... args ) => { if ( timer ) clearTimeout ( timer ) ; timer = setTimeout ( ( ) => { func . apply ( this , args ) ; } , awai ) ; } ; } ; public getUrlKey ( name ) { return decodeURIComponent ( ( new RegExp ( '[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)' ) . exec ( location . href ) || [ , "" ] ) [ 1 ] . replace ( /\+/g , '%20' ) ) || null ; } public isObjectLike : currencyInterface < any > = ( val ) => { return Object . prototype . toString . call ( val ) === '[object Object]' ; } ; public cached ( fn ) { let cache = Object . create ( null ) ; return function cachedFn ( str ) { let hit = cache [ str ] ; return hit || ( cache [ str ] = fn ( str ) ) ; } ; } public capitalize : currencyInterface < string > = ( val ) => { return val . charAt ( 0 ) . toUpperCase ( ) + val . slice ( 1 ) ; } ; public getExplorerInfo ( ) { let t = navigator . userAgent . toLowerCase ( ) ; return 0 <= t . indexOf ( "msie" ) ? { type : "IE" , version : Number ( ( t . match ( /msie ([\d]+)/ ) as any ) [ 1 ] ) } : ! ! t . match ( /trident\/.+?rv:(([\d.]+))/ ) ? { type : "IE" , version : 11 } : 0 <= t . indexOf ( "edge" ) ? { type : "Edge" , version : Number ( ( t . match ( /edge\/([\d]+)/ ) as any ) [ 1 ] ) } : 0 <= t . indexOf ( "firefox" ) ? { type : "Firefox" , version : Number ( ( t . match ( /firefox\/([\d]+)/ ) as any ) [ 1 ] ) } : 0 <= t . indexOf ( "chrome" ) ? { type : "Chrome" , version : Number ( ( t . match ( /chrome\/([\d]+)/ ) as any ) [ 1 ] ) } : 0 <= t . indexOf ( "opera" ) ? { type : "Opera" , version : Number ( ( t . match ( /opera.([\d]+)/ ) as any ) [ 1 ] ) } : 0 <= t . indexOf ( "Safari" ) ? { type : "Safari" , version : Number ( ( t . match ( /version\/([\d]+)/ ) as any ) [ 1 ] ) } : { type : t , version : - 1 } ; } public Broswer ( ) { let e = navigator . userAgent . toLowerCase ( ) , t = "ipad" == e . match ( /ipad/i ) , i = "iphone" == e . match ( /iphone os/i ) , r = "midp" == e . match ( /midp/i ) , n = "rv:1.2.3.4" == e . match ( /rv:1.2.3.4/i ) , a = "ucweb" == e . match ( /ucweb/i ) , o = "android" == e . match ( /android/i ) , s = "windows ce" == e . match ( /windows ce/i ) , l = "windows mobile" == e . match ( /windows mobile/i ) ; return ! ( t || i || r || n || a || o || s || l ) ; } public getRawType ( val ) { return Object . prototype . toString . call ( val ) . slice ( 8 , - 1 ) ; } public isStatic : currencyInterface < any > = ( val ) => { return ( typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean' || typeof val === 'undefined' || val === null ) ; } ; public unique ( arr ) { if ( ! Array . isArray ( arr ) ) { return arr ; } let resultArr = [ ] ; let oar = [ ] ; let obj = Object . create ( null ) ; arr . forEach ( item => { if ( this . isStatic ( item ) ) { let key = item + '_' + this . getRawType ( item ) ; if ( ! obj [ key ] ) { obj [ key ] = true ; resultArr . push ( item ) ; } } else { if ( ! oar . includes ( item ) ) { oar . push ( item ) ; resultArr . push ( item ) ; } } } ) ; return resultArr ; } public disableCopySelection ( type ? ) { type = type || 'all' ; [ 'contextmenu' , 'selectstart' , 'copy' ] . forEach ( function ( ev ) { if ( type === ev ) { document . addEventListener ( ev , function ( event ) { return event . returnValue = false ; } ) ; } if ( type === 'all' ) { document . addEventListener ( ev , function ( event ) { return event . returnValue = false ; } ) ; } } ) ; } public viewWebPagePerformance ( ) { window . onload = function ( ) { setTimeout ( function ( ) { let t = performance . timing ; console . log ( 'DNS查询耗时 ：' + ( t . domainLookupEnd - t . domainLookupStart ) . toFixed ( 0 ) + 'ms' ) ; console . log ( 'TCP链接耗时 ：' + ( t . connectEnd - t . connectStart ) . toFixed ( 0 ) + 'ms' ) ; console . log ( 'request请求耗时 ：' + ( t . responseEnd - t . responseStart ) . toFixed ( 0 ) + 'ms' ) ; console . log ( '解析dom树耗时 ：' + ( t . domComplete - t . domInteractive ) . toFixed ( 0 ) + 'ms' ) ; console . log ( '白屏时间 ：' + ( t . responseStart - t . navigationStart ) . toFixed ( 0 ) + 'ms' ) ; console . log ( 'domready时间 ：' + ( t . domContentLoadedEventEnd - t . navigationStart ) . toFixed ( 0 ) + 'ms' ) ; console . log ( 'onload时间 ：' + ( t . loadEventEnd - t . navigationStart ) . toFixed ( 0 ) + 'ms' ) ; if ( t = performance . memory ) { console . log ( 'js内存使用占比 ：' + ( t . usedJSHeapSize / t . totalJSHeapSize * 100 ) . toFixed ( 2 ) + '%' ) ; } } ) ; } ; } }