declare namespace FunctionUtil { /** * 函数柯里化 * @param { Function } fn 方法 * @param { Any } agrs 参数,可选 * @example * // 实现一个判断数据类型的方法 const checktype = function(type, content) { return Object.prototype.toString.call(content) === `[object ${type}]`; } checktype('Number',2); => true // 这种方法总是要把type参数传过去,如果写错了就会影响到正确的结果了,可以考虑下如何做到把“Number“做到复用 const checktype = function(type, content) { return Object.prototype.toString.call(content) === `[object ${type}]`; } const curry = curryFn(checktype) const isNumber = curry('Number') isNumber(2) => true */ function curryFn(fn: any, ...arg: any): () => any; /** * 函数防抖 * @param fn 需要防抖的函数 * @param t 防抖时间,多久以后才能再执行 单位ms * @param immediate true: 立刻执行方法且最后一次时间不执行, false: 等t时间之后再执行方法,如果t时间内执行,则在最后一次的t时间之后执行方法,类似动态搜索效果 * @example * const query = debounceFn(()=>console.log(3),2000,true) * query() => 3 * query() * query() * const query = debounceFn(()=>console.log(3),2000,false) * query() * query() * query() => 3 */ function debounceFn(fn: T, t?: number, immediate?: boolean): T; /** * 函数节流 * @param fn 需要节流的函数 * @param t 节流时间,多久以后执行一次方法 单位ms * @example * const query = throttleFn(()=>console.log(3),2000) * query() => 3 * query() * query() */ function throttleFn(fn: T, t?: number): T; /** * 函数只能调用一次 * @param { Function } fn 需要被处理的函数 * @example * const canOnlyFireOnce = once(()=>console.log(1)); * for(let i =1;i<10;i++){ * canOnlyFireOnce(); * } * // 1 */ function once(fn: T): T; /** * 睡眠函数, 阻塞代码 timer毫秒 * @param timer 睡眠时长 (毫秒) 执行后续的操作 * @return promise * @example * const fn = async () =>{ * console.log(1) * await sleepFn(2000) * console.log(2) * } * fn() * 1 * //两秒后 * 2 */ function sleepFn(timer?: number): Promise; } export default FunctionUtil;