判断数组中所有数是否满足条件
const all = ( arr, fn = Boolean ) => arr. every ( fn) ;
all ( [ 4 , 2 , 3 ] , x => x > 1 ) ;
all ( [ 1 , 2 , 3 ] ) ;
判断数组中是否有一个满足条件
const any = ( arr, fn = Boolean ) => arr. some ( fn) ;
any ( [ 0 , 1 , 2 , 0 ] , x => x >= 2 ) ;
any ( [ 0 , 0 , 1 , 0 ] ) ;
过滤数据中满足条件的数
const dropRightWhile = ( arr, func ) => {
let rightIndex = arr. length;
while ( rightIndex-- && ! func ( arr[ rightIndex] ) ) ;
return arr. slice ( 0 , rightIndex + 1 ) ;
} ;
dropRightWhile ( [ 1 , 2 , 3 , 4 ] , n => n < 3 ) ;
判断数组中是否有重复
const allUnique = arr => arr. length === new Set ( arr) . size;
allUnique ( [ 1 , 2 , 3 , 4 ] ) ;
allUnique ( [ 1 , 1 , 2 , 3 ] ) ;
base64转码
window. atob ( 'Zm9vYmFy' )
window. btoa ( 'foobar' )
判断是否触底
const bottomVisible = ( ) =>
document. documentElement. clientHeight + window. scrollY >=
( document. documentElement. scrollHeight ||
document. documentElement. clientHeight) ;
bottomVisible ( ) ;
回到顶部
const scrollToTop = ( ) => {
const c = document. documentElement. scrollTop || document. body. scrollTop;
if ( c > 0 ) {
window. requestAnimationFrame ( scrollToTop) ;
window. scrollTo ( 0 , c - c / 8 ) ;
}
} ;
scrollToTop ( ) ;
复制
const copyToClipboard = str => {
const el = document. createElement ( 'textarea' ) ;
el. value = str;
el. setAttribute ( 'readonly' , '' ) ;
el. style. position = 'absolute' ;
el. style. left = '-9999px' ;
document. body. appendChild ( el) ;
const selected =
document. getSelection ( ) . rangeCount > 0
? document. getSelection ( ) . getRangeAt ( 0 )
: false ;
el. select ( ) ;
document. execCommand ( 'copy' ) ;
document. body. removeChild ( el) ;
if ( selected) {
document. getSelection ( ) . removeAllRanges ( ) ;
document. getSelection ( ) . addRange ( selected) ;
}
} ;
copyToClipboard ( 'Lorem ipsum' ) ;
判断是否移动端
const detectDeviceType = ( ) =>
/ Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini / i . test (
navigator. userAgent
)
? 'Mobile'
: 'Desktop' ;
detectDeviceType ( ) ;
判断浏览器语言
const detectLanguage = ( defaultLang = 'en-US' ) =>
navigator. language ||
( Array. isArray ( navigator. languages) && navigator. languages[ 0 ] ) ||
defaultLang;
detectLanguage ( ) ;
获取网址 不带参
const getBaseURL = url => url. replace ( / [?#].*$ / , '' ) ;
getBaseURL ( 'http://url.com/page?name=Adam&surname=Smith' ) ;
获取url参数 二选一
const getURLParameters = url =>
( url. match ( / ([^?=&]+)(=([^&]*)) / g ) || [ ] ) . reduce (
( a, v ) => (
( a[ v. slice ( 0 , v. indexOf ( '=' ) ) ] = v. slice ( v. indexOf ( '=' ) + 1 ) ) , a
) ,
{ }
) ;
const queryStringToObject = url =>
[ ... new URLSearchParams ( url. split ( '?' ) [ 1 ] ) ] . reduce (
( a, [ k, v] ) => ( ( a[ k] = v) , a) ,
{ }
) ;
getURLParameters ( 'google.com' ) ;
getURLParameters ( 'http://url.com/page?name=Adam&surname=Smith' ) ;
颜色hex转rgba
const hexToRGB = hex => {
let alpha = false ,
h = hex. slice ( hex. startsWith ( '#' ) ? 1 : 0 ) ;
if ( h. length === 3 ) h = [ ... h] . map ( x => x + x) . join ( '' ) ;
else if ( h. length === 8 ) alpha = true ;
h = parseInt ( h, 16 ) ;
return (
'rgb' +
( alpha ? 'a' : '' ) +
'(' +
( h >>> ( alpha ? 24 : 16 ) ) +
', ' +
( ( h & ( alpha ? 0x00ff0000 : 0x00ff00 ) ) >>> ( alpha ? 16 : 8 ) ) +
', ' +
( ( h & ( alpha ? 0x0000ff00 : 0x0000ff ) ) >>> ( alpha ? 8 : 0 ) ) +
( alpha ? ` , ${ h & 0x000000ff } ` : '' ) +
')'
) ;
} ;
hexToRGB ( '#27ae60ff' ) ;
hexToRGB ( '27ae60' ) ;
hexToRGB ( '#fff' ) ;
保留小数位数,四舍五入
const round = ( n, decimals = 0 ) =>
Number ( ` ${ Math. round ( ` ${ n} e ${ decimals} ` ) } e- ${ decimals} ` ) ;
round ( 1.005 , 2 ) ;
点击外部关闭弹框
const onClickOutside = ( element, callback ) => {
document. addEventListener ( 'click' , e => {
if ( ! element. contains ( e. target) ) callback ( ) ;
} ) ;
} ;
onClickOutside ( '#my-element' , ( ) => console. log ( 'Hello' ) ) ;
httpGet
const httpGet = ( url, callback, err = console. error ) => {
const request = new XMLHttpRequest ( ) ;
request. open ( 'GET' , url, true ) ;
request. onload = ( ) => callback ( request. responseText) ;
request. onerror = ( ) => err ( request) ;
request. send ( ) ;
} ;
httpGet (
'https://jsonplaceholder.typicode.com/posts/1' ,
console. log
) ;
httpPost
const httpPost = ( url, data, callback, err = console. error ) => {
const request = new XMLHttpRequest ( ) ;
request. open ( 'POST' , url, true ) ;
request. setRequestHeader ( 'Content-type' , 'application/json; charset=utf-8' ) ;
request. onload = ( ) => callback ( request. responseText) ;
request. onerror = ( ) => err ( request) ;
request. send ( data) ;
} ;
const newPost = {
userId: 1 ,
id: 1337 ,
title: 'Foo' ,
body: 'bar bar bar'
} ;
const data = JSON . stringify ( newPost) ;
httpPost (
'https://jsonplaceholder.typicode.com/posts' ,
data,
console. log
) ;
httpPost (
'https://jsonplaceholder.typicode.com/posts' ,
null ,
console. log
) ;
httpPut
const httpPut = ( url, data, callback, err = console. error ) => {
const request = new XMLHttpRequest ( ) ;
request. open ( 'PUT' , url, true ) ;
request. setRequestHeader ( 'Content-type' , 'application/json; charset=utf-8' ) ;
request. onload = ( ) => callback ( request) ;
request. onerror = ( ) => err ( request) ;
request. send ( data) ;
} ;
const password = 'fooBaz' ;
const data = JSON . stringify ( {
id: 1 ,
title: 'foo' ,
body: 'bar' ,
userId: 1
} ) ;
httpPut ( 'https://jsonplaceholder.typicode.com/posts/1' , data, request => {
console. log ( request. responseText) ;
} ) ;
httpDelete
const httpDelete = ( url, callback, err = console. error ) => {
const request = new XMLHttpRequest ( ) ;
request. open ( 'DELETE' , url, true ) ;
request. onload = ( ) => callback ( request) ;
request. onerror = ( ) => err ( request) ;
request. send ( ) ;
} ;
httpDelete ( 'https://jsonplaceholder.typicode.com/posts/1' , request => {
console. log ( request. responseText) ;
} ) ;
获取坐标
const getScrollPosition = ( el = window ) => ( {
x: el. pageXOffset !== undefined ? el. pageXOffset : el. scrollLeft,
y: el. pageYOffset !== undefined ? el. pageYOffset : el. scrollTop
} ) ;
getScrollPosition ( ) ;