noRepeat(arr) {
return [...new Set(arr)]
}
arrayMax(arr) {
return Math.max(...arr);
}
arrayMin(arr) {
return Math.min(...arr);
}
chunk(arr, size = 1) {
return Array.from({
length: Math.ceil(arr.length / size)
}
(v, i) => arr.slice(i * size, i * size + size));
}
countOccurrences(arr, value) {
return arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
}
flatten(arr, depth = -1) {
if (depth === -1) {
return [].concat(...arr.map(v => Array.isArray(v) ? this.flatten(v) : v))
}
if (depth === 1) {
return arr.reduce((a, v) => a.concat(v), []);
}
return arr.reduce((a, v) => a.concat(Array.isArray(v) ? this.flatten(v, depth - 1) : v), [])
}
diffrence(arrA, arrB) {
return arrA.filter(v => !arrB.includes(v))
}
intersection(arr1, arr2) {
return arr2.filter(v => arr1.includes(v))
}
dropRight(arr, n = 0) {
return n < arr.length ? arr.slice(0, arr.length - n) : [];
}
dropElements(arr, fn) {
while (arr.length && !fn(arr[0])) arr = arr.slice(1);
return arr;
}
everyNth(arr, nth) {
return arr.filter((v, i) => i % nth === nth - 1)
}
nthElement(arr, n = 0) {
return (n >= 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]
}
head(arr) {
return arr[0]
}
判断当前位置是否为页面底部,返回值为true/false
bottomVisible() {
return document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);
}
currentURL() {
return window.location.href
}
getScrollPosition(el = window) {
return {
x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop
}
}
getURLParameters(url) {
return url.match(/([^?=&]+)(=([^&]*))/g).reduce(
(a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {}
)
}
redirect(url, asLink = true) {
asLink ? window.location.href = url : window.location.replace(url)
}
scrollToTop() {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
} else {
window.cancelAnimationFrame(scrollToTop);
}
}
timestampToTime(timestamp = Date.parse(new Date()), isPHP = false) {
const date = new Date(timestamp * (isPHP ? 1000 : 1))
return `${date.getFullYear()}-${date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
}
判断元素是否在可视范围内, partiallyVisible为是否为完全可见
elementIsVisibleInViewport(el, partiallyVisible = false) {
const {
top,
left,
bottom,
right
} = el.getBoundingClientRect();
return partiallyVisible ?
((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) :
top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
}