Skip to content

Instantly share code, notes, and snippets.

rxliuli rxliuli

Block or report user

Report or block rxliuli

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View CSS 隐藏多余的文字引起错位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
.box {
width: 500px;
View CSS 权重
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
span {
color: green;
@rxliuli
rxliuli / 数组分组
Last active May 30, 2019
将数组按照最大值进行分组,分组次数尽可能少,分组数量尽可能少
View 数组分组
function split(i: number, max: number): number[] {
const res = []
while (i > max) {
res.push(max)
i -= max
}
res.push(i)
return res
}
function fn(arr: number[], max: number): number[][] {
View gist:09c54bd9325a641b90f4d0d6c284f437
/**
* 不关心参数的函数类型
* @typeparam R 函数的返回值类型
*/
export type ReturnFunc<R = any> = (...args: any[]) => R
/**
* 谓词的返回值,支持异步函数
*/
type PredicateReturn = boolean | Promise<boolean>
View curry
/**
* 将函数包装为柯里化函数
* 注: 该函数模仿了 Lodash 的 curry 函数
* @param {Function} fn 需要包装的函数
* @param {...any} args 应用的部分参数
View groupBy
export type ArrayCallback<T, R> = (item: T, index?: number, arr?: T[]) => R
export function convert<T, R>(t: T): R {
return (t as unknown) as R
}
/**
* js 数组按照某个条件进行分组
*
* @param {Array<Object>} arr 要进行分组的数组
View treeMapping
/**
* 判断一个对象是否是无效的
* 无效的值仅包含 null/undefined
* @param object 任何一个对象
* @returns 是否无效的值
*/
export function isNullOrUndefined(object: any): boolean {
return object === undefined || object === null
}
View wait
const wait = param => {
return new Promise(resolve => {
if (typeof param === 'number') {
setTimeout(resolve, param)
} else if (typeof param === 'function') {
const timer = setInterval(() => {
if (param()) {
clearInterval(timer)
resolve()
}
View debounce
// 使用 Promise 简单封装 setTimeout
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
;(async () => {
// 同步
function debounce(delay, action, init = undefined) {
let flag
let result = init
return function(...args) {
if (flag) clearTimeout(flag)
View compose
/**
* 将函数包装为柯里化函数
* @param {Function} fn 需要包装的函数
* @param {...any} args 应用的部分参数
* @returns {Function} 包装后的函数
*/
export const curry = (fn, ...args) => {
const realArgs = args.filter(arg => arg !== curry._)
if (realArgs.length >= fn.length) {
return fn(...realArgs)
You can’t perform that action at this time.