Skip to content

Instantly share code, notes, and snippets.

View Averey's full-sized avatar
🎯
Focusing

Averey

🎯
Focusing
View GitHub Profile
// 扁平化数组
// const a = [1, [2, [3,4]]]; flaten(a)
const flaten = arr =>
arr.reduce(
(acc, cur) => Array.isArray(cur)?acc.concat(...fl(cur)):acc.concat(cur)
,[])
// 判断数组中某个值出现的次数
function countOccurrences(arr, value) {
return arr.reduce((a, v) => (v === value ? a + 1 : a + 0), 0);
@Averey
Averey / hooks.js
Last active April 30, 2020 03:52
common use hook in reactjs
import { useState, useEffect } from "react";
/**
* 根据 kw 过滤 list 的数据。用于纯前端数据搜索
* initList: 没筛选时的初始列表
* kw: 关键字,
* list: 返回的结果列表
* keys: list 的 item 中用于和 kw 匹配的key
*/
function useSearchFilter(keys, _initlist) {
.text-ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
@Averey
Averey / fns.js
Last active March 11, 2022 10:56
functions utils
export const idUq = id => item => item.id !== id;
export const idEq = id => item => item.id === id;
export const getId = item => item.id;
export const callFn = (fn, ...params) => {
if (typeof fn === 'function') {
return fn.apply(null, params)
}
}
export const a2z = (isUpper = false) => {
@Averey
Averey / collection.js
Last active April 28, 2020 02:54
collection utils
// check list or object is null or empty
const isEmpty = obj = !obj || Object.keys(obj).length === 0;
export const isEmptyList = list => !list || list.length === 0;
export const isSameArray = (a1, a2) => {
if ( isEmptyList(a1) && isEmptyList(a2)) return true;
if (isEmptyList(a1) && !isEmptyList(a2)) return false;
if (!isEmptyList(a1) && isEmptyList(a2)) return false;
if (a1.length !== a2.length) return false;
@Averey
Averey / formatDate.js
Last active May 31, 2022 03:17
format timestamp
export function formatTime(_date, fm = 'YY.MM.DD hh:mm:ss') {
let d
if (Object.prototype.toString.call(_date).toLocaleLowerCase() === '[object date]') {
d = _date
} else {
d = new Date(_date)
}
let year = d.getFullYear()
let month = d.getMonth() + 1
@Averey
Averey / htmlTagRegex.js
Created November 14, 2018 10:36
html tag regex
const str = "<p></p>"
/<[^>]+>/i.test(str);
@Averey
Averey / getUrlParams.js
Created June 22, 2018 10:41
parse url params to KV object
const params = (function() {
const array = location.search.substr(1).split('&')
const obj = {}
array.forEach(item => {
const a = item.split('=')
const k = a[0]
const v = a[1]
obj[k] = v
})
return obj
@Averey
Averey / scrollOnBottom.js
Last active June 8, 2018 08:07
Check if the user is scrolled to the bottom of the page.
window.onscroll = function() {
const documentEle = document.documentElement;
const offset = documentEle.scrollTop + window.innerHeight;
const height = documentEle.scrollHeight;
if (offset === height) {
console.log('At the bottom');
}
}