Skip to content

Instantly share code, notes, and snippets.

View ExcitedSpider's full-sized avatar

qefeng ExcitedSpider

View GitHub Profile
@ExcitedSpider
ExcitedSpider / useSearchParams.ts
Created February 11, 2022 04:14
创建状态并持久化到 search param 中
import { snakeCase } from 'lodash';
import { camelCase, kebabCase, mapKeys, transform } from 'lodash/fp';
import React, { useEffect } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
export type SearchParamObject<ParamList extends string> = {
[key in ParamList]: string;
};
export interface SetSearchParamAction<ParamList extends string> {
@ExcitedSpider
ExcitedSpider / async-reduce.ts
Created December 13, 2021 12:56
array reduce in async way
export async function asyncReduce<IterationItem, AccValue>(
array: IterationItem[],
reducer: (accumulate: AccValue, current: IterationItem) => AccValue | Promise<AccValue>,
initValue: AccValue,
): Promise<AccValue> {
let accumulate = typeof initValue === 'object' ? Object.create((initValue as unknown) as object) : initValue;
for (const item of array) {
accumulate = await reducer(accumulate, item);
}
@ExcitedSpider
ExcitedSpider / use-vscroll.ts
Created August 2, 2021 06:35
A react hook to make a virtual scroll container
import React, { useState, useEffect, useRef } from 'react';
import clamp from 'lodash/clamp';
export type VScrollController = { scrollTo: (top: number) => void };
const DEFAULT_DURATION = '200ms';
const DEFAULT_TIMEING_FUNCTION = 'ease-in-out';
/**
* virtual scroll 的实现
@ExcitedSpider
ExcitedSpider / use-echarts.ts
Created August 2, 2021 06:34
simple hooks to use echarts
export interface UseEchartsOptions {
/**
* render root
*/
renderRootRef: React.RefObject<HTMLDivElement | HTMLCanvasElement>;
/**
* `setOption` 1st param object
*/
echartsOption: EChartOption;
/**
@ExcitedSpider
ExcitedSpider / camelCase.js
Created February 22, 2021 06:11 — forked from johnsmith17th/camelCase.js
To convert string to camel case in javascript.
function toCamelCase(str) {
return str.toLowerCase().replace(/(?:(^.)|(\s+.))/g, function(match) {
return match.charAt(match.length-1).toUpperCase();
});
}
@ExcitedSpider
ExcitedSpider / debounce.ts
Created February 20, 2021 06:17
simple debounce & throttle
export const debounce: <T extends []>(fn: (...args: T) => void, wait: number) => (...args: T) => void = (fn, wait) => {
let timeout = -1;
return (...args) => {
if (timeout !== -1) clearTimeout(timeout);
timeout = setTimeout(() => {
fn(...args);
}, wait);
};
};
@ExcitedSpider
ExcitedSpider / rollup-plugin-file-path.js
Created December 21, 2020 08:27
Get the path of local file. Load any type of file.
const { createFilter } = require("@rollup/pluginutils");
const pathTemplate =(path)=> `
var path = ${path};
export default path;
`
module.exports = function filePath(
options = {
include: ["**/*.ejs"],
@ExcitedSpider
ExcitedSpider / vue-component-capture.ts
Created December 3, 2020 07:30
capture a vue component with a callback
/**
* 可选的 组件注销时调用的回调函数
*
* 可以用来清理副作用,避免内存泄漏
*/
type HandlerCleanup = VoidFunction
/**
* 组件捕获后执行的回调函数
*/
@ExcitedSpider
ExcitedSpider / polling.ts
Last active November 30, 2020 08:36
Util polling method. Use `requestIdleCallback` first. If user agent not support (e.g. safari, ie), use `setInterval` instead
const DEFAULT_POLLING_INTERVAL = 300
interface Polling {
(cb: () => void, timeout?: number): {
/** stop polling */
stopPolling: () => void
}
}
/**

Flow语法入门

qyfeng03

在文件中开启flow检查

// @flow