生成一周时间
function getWeekTime ( ) {
return [ ... new Array ( 7 ) ] . map ( ( j, i) => new Date ( Date. now ( ) + i* 8.64e7 ) . toLocaleDateString ( ) )
}
类型判断
function isType ( target, type) {
let targetType = Object. prototype. toString. call ( target) . slice ( 8 , - 1 ) . toLowerCase ( )
return targetType === type. toLowerCase ( )
}
isType ( [ ] , 'Array' )
isType ( /\d/ , 'RegExp' )
isType ( new Date ( ) , 'Date' )
isType ( function ( ) { } , 'Function' )
isType ( Symbol ( 1 ) , 'Symbol' )
对象属性剔除
function omit ( object, props= [ ] ) {
let res = { }
Object. keys ( object) . forEach ( key=> {
if ( props. includes ( key) === false ) {
res[ key] = typeof object[ key] === 'object' && object[ key] !== null ?
jsON. parse ( jsON. stringify ( object[ key] ) ) :
object[ key]
}
} )
return res
}
数组去重
function removeDupthird ( arr) {
var newArr = [ ] ;
var obj = { } ;
for ( var i = 0 ; i < arr. length; i++ ) {
obj[ arr[ i] ] = arr[ i] ;
}
var list = [ ] ;
for ( var i in obj) {
list. push ( i) ;
}
return list;
} ;
let data = {
id: 1 ,
title: 'xxx' ,
comment: [ ]
}
omit ( data, [ 'id' ] )
日期格式化
function formatDate ( format= 'Y-M-D h:m' , timestamp= Date. now ( ) ) {
let date = new Date ( timestamp)
let dateInfo = {
Y : date. getFullYear ( ) ,
M : date. getMonth ( ) + 1 ,
D : date. getDate ( ) ,
h: date. getHours ( ) ,
m: date. getMinutes ( ) ,
s: date. getSeconds ( )
}
let formatNumber = ( n) => n > 10 ? n : '0' + n
let res = format
. replace ( 'Y' , dateInfo. Y )
. replace ( 'M' , dateInfo. M )
. replace ( 'D' , dateInfo. D )
. replace ( 'h' , formatNumber ( dateInfo. h) )
. replace ( 'm' , formatNumber ( dateInfo. m) )
. replace ( 's' , formatNumber ( dateInfo. s) )
return res
}
formatDate ( )
formatDate ( 'M月D日 h:m' )
formatDate ( 'h:m Y-M-D' , 1582526221604 )
防抖
function debounce ( func, wait = 300 ) {
let timer = null ;
return function ( ) {
if ( timer !== null ) {
clearTimeout ( timer) ;
}
timer = setTimeout ( fn, wait) ;
}
}
function debounce ( func, wait = 300 , immediate = false ) {
let timer, ctx;
let later = ( arg) => setTimeout ( ( ) => {
func. apply ( ctx, arg)
timer = ctx = null
} , wait)
return function ( ... arg) {
if ( ! timer) {
timer = later ( arg)
ctx = this
if ( immediate) {
func. apply ( ctx, arg)
}
} else {
clearTimeout ( timer)
timer = later ( arg)
}
}
}
let scrollHandler = debounce ( function ( e) {
console. log ( e)
} , 500 )
window. onscroll = scrollHandler
节流
function throttle ( func, delay) {
let timer = null
return function ( ... arg) {
if ( ! timer) {
timer = setTimeout ( ( ) => {
func. apply ( this , arg)
timer = null
} , delay)
}
}
}
let scrollHandler = throttle ( function ( e) {
console. log ( e)
} , 500 )
window. onscroll = scrollHandler
base64数据导出文件下载
function downloadFile ( filename, data) {
let downloadLink = document. createElement ( 'a' ) ;
if ( downloadLink ) {
document. body. appendChild ( downloadLink) ;
downloadLink. style = 'display: none' ;
downloadLink. download = filename;
downloadLink. href = data;
if ( document. createEvent ) {
let downloadEvt = document. createEvent ( 'MouseEvents' ) ;
downloadEvt. initEvent ( 'click' , true , false ) ;
downloadLink. dispatchEvent ( downloadEvt) ;
} else if ( document. createEventObject ) {
downloadLink. fireEvent ( 'onclick' ) ;
} else if ( typeof downloadLink. onclick == 'function' ) {
downloadLink. onclick ( ) ;
}
document. body. removeChild ( downloadLink) ;
}
}
检测是否为PC端浏览器
function isPCBroswer ( ) {
let e = window. navigator. userAgent. toLowerCase ( )
, t = "ipad" == e. match ( /ipad/i )
, i = "iphone" == e. match ( /iphone/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)
}
识别浏览器及平台
function getPlatformInfo ( ) {
let inBrowser = typeof window !== 'undefined' ;
let inWeex = typeof WXEnvironment !== 'undefined' && ! ! WXEnvironment. platform;
let weexPlatform = inWeex && WXEnvironment. platform. toLowerCase ( ) ;
let UA = inBrowser && window. navigator. userAgent. toLowerCase ( ) ;
if ( UA ) {
let platforms = {
IE : /msie|trident/ . test ( UA ) ,
IE9 : UA . indexOf ( 'msie 9.0' ) > 0 ,
Edge: UA . indexOf ( 'edge/' ) > 0 ,
Android: UA . indexOf ( 'android' ) > 0 || ( weexPlatform === 'android' ) ,
IOS : /iphone|ipad|ipod|ios/ . test ( UA ) || ( weexPlatform === 'ios' ) ,
Chrome: /chrome\/\d+/ . test ( UA ) && ! ( UA . indexOf ( 'edge/' ) > 0 ) ,
}
for ( const key in platforms) {
if ( platforms. hasOwnProperty ( key) ) {
if ( platforms[ key] ) return key
}
}
}
}
获取系统根路径
function getRootPath ( ) {
var curWwwPath = window. document. location. href;
var pathName = window. document. location. pathname;
var pos = curWwwPath. indexOf ( pathName) ;
var localhostPaht = curWwwPath. substring ( 0 , pos) ;
var projectName = pathName. substring ( 0 , pathName. substring ( 1 ) . indexOf ( '/' ) + 1 ) ;
return ( localhostPaht) ;
}