import { AsyncFn, Expand, FlipLoose, Fn, Function, Stringable } from "@wai-ri/shared"; export * from "@wai-ri/shared"; //#region asyncDebounce/index.d.ts type DebounceOption = { /** 最大等待时间,在不停触发的情况下,至少多少毫秒调用一次,类似节流 */ maxWait?: number; }; /** * 异步防抖 可.then * * @example * const debouncedAsyncFn = asyncDebounce(someAsyncFn, 200, { maxWait: 1000 }) * * function onSearch() { * const id = 1 * debouncedAsyncFn(id) * .then(data => doSomething(data)) * .catch(err => catchErr(err)) * } */ declare function asyncDebounce(asyncFn: Fn, wait?: number, options?: DebounceOption): (...args: Params) => Promise; //#endregion //#region backward/index.d.ts /** * Iterates over an array backward. * @example * ```ts * const array = [1, 2, 3] * for (const item of backward(array)) { * console.log(item) * } * // 3 2 1 * ``` */ declare function backward(array: ArrayLike): IterableIterator; declare function backwardEntries(array: ArrayLike): IterableIterator; //#endregion //#region bindAll/index.d.ts /** * 绑定对象上的所有函数 * * @note 这会修改原对象 * * @example * ```ts * class Example { * count = 0 * increase() { * this.count++ * return this.count * } * } * * const example = new Example() * bindAll(example) * const increase = example.increase * console.log(increase()) // 1 * console.log(increase()) // 2 * ``` */ declare function bindAll>(obj: T): T; type ExtactFnProp> = { [K in keyof T]: T[K] extends Fn ? K : never; }; /** * 获取对象上所有函数,并生成新函数 * * @example * ```ts * class Example { * count = 0 * increase() { * this.count++ * return this.count * } * } * * const example = new Example() * const bound = toBound(example) * const increase = bound.increase * console.log(increase()) // 1 * console.log(increase()) // 2 * ``` */ declare function toBound>(obj: T): ExtactFnProp; //#endregion //#region bindSelf/index.d.ts /** * 绑定对象函数 this 为对象本身 */ declare function bindSelf, K extends keyof T>(obj: T, key: T[K] extends Fn ? K : never): T[K]; declare function bindSelf>(obj: T, key: (keyof T)[]): T; //#endregion //#region blockThread/index.d.ts /** * 阻塞 JS 线程 * @param ms 阻塞时长 * ```ts * // 模拟 cpu 长耗时任务 * blockThread(200) * ``` */ declare function blockThread(ms?: number): void; //#endregion //#region clamp/index.d.ts type MaybeNumber = number | undefined | null; /** 限定数值大小 */ declare function clamp(n: number, min: number, max: number): number; /** clamp Array version */ declare function clampArray(valueArr: T, min: number, max: number): T; //#endregion //#region collapseWhitespace/index.d.ts /** 去除多余的空格(首尾空格去除,中间空格合并) */ declare function collapseWhitespace(val: string): string; //#endregion //#region cssCustomHighlight/index.d.ts type Options = { /** * @default false */ caseSensitive?: boolean; /** * @default true */ trim?: boolean; /** * */ name: string; }; /** * * @param node * @param searchText * @param options * @returns function to remove the highlight */ declare function nativeHighlight(node: HTMLElement, searchText: string, options: Options): () => void; //#endregion //#region getAllKeys/index.d.ts /** 获取所有属性名,类似 Reflect.ownKeys,但会检查原型链(不包括Objec.prototype) */ declare function getAllKeys(obj: Record): (string | symbol)[]; //#endregion //#region ignoreReject/index.d.ts /** 忽略 Promise 的 .catch 错误,只有 resolve 会继续传递 */ declare function ignoreReject(promise: Promise, errorHandle?: Fn): Promise; //#endregion //#region lerp/index.d.ts /** * 插值 * @param start 开始值 * @param stop 结束值 * * ```ts * const fn = lerp(10, 20) * console.log(fn(0.5)) // 15 * ``` */ declare function lerp(start: number, stop: number): (amt: number) => number; /** * 插值 * @param start 开始值 * @param stop 结束值 * @param amt 比例 0 - 1 * * ```ts * console.log(lerp(10, 20, 0.5)) // 15 * ``` */ declare function lerp(start: number, stop: number, amt: number): number; //#endregion //#region map/index.d.ts declare function mapAtIndex(map: Map, index: number): [K, V] | undefined | void; declare function mapIndexOfKey(map: Map, key: K): number; //#endregion //#region noop/index.d.ts /** 顾名思义,什么也不做 */ declare function noop(...args: any[]): void; //#endregion //#region patternMatching/index.d.ts type GetMatchArmResults = Result extends (infer SubResult) ? SubResult extends ((...args: any) => infer Return) ? Return : SubResult : never; /** * 模式匹配 * {@link FALLBACK_ARM default 分支} * @example * ```ts * const value = "a" as unknown * * // ↓ 'a' | 'b' | 'c' * const result = patternMatching(value, { * a: () => 'a', * b: "b", * _: "c" * }) * ``` */ declare function patternMatching), unknown>>>(value: K, matchArms: T): GetMatchArmResults; //#endregion //#region pipe/index.d.ts /** 管道函数 参数类型 */ type PipeParams = Len extends 0 ? [] : Funcs extends [infer First extends Function.UnaryFn, infer Second extends Function.UnaryFn, ...infer Rest extends Function.UnaryFn[]] ? [Function.SetParams, ...PipeParams<[Function.SetParams]>, ...Rest], ReturnType>] : Funcs; /** 管道函数 返回类型 */ type PipeReturn = Funcs extends [...unknown[], (arg: any) => infer R] ? R : FirstArg; declare function pipe(startValue: T): (...fns: PipeParams) => PipeReturn; //#endregion //#region sleep/index.d.ts /** 等待 0 毫秒 */ declare function sleep(): Promise; /** 等待`ms`毫秒 */ declare function sleep(ms: number): Promise; /** 等待`ms`毫秒后执行函数 */ declare function sleep

(ms: number, fn: Fn, ...args: P): Promise>; //#endregion //#region switchLatest/index.d.ts /** 切换到最后一次调用 */ declare function switchLatest

Promise>(fn: T): T; /** * 切换到最后一次,但会根据参数决定,只有参数ID相同且不为最后一次会被舍弃 * @param fn 包装的函数 * @param identity 获取 ID 函数 * ```ts * function getTarget(id: string) { * return axios.get("/api/target", { params: { id } }) * } * * const getTarget_switchLatest = switchLatestWith(getTarget, id => id) * * function getAndSetTarget(id: string) { * getTarget_switchLatest(id).then(data => store.set(id, data)) * } * ``` */ declare function switchLatestWith(fn: AsyncFn, identity: Fn): AsyncFn; //#endregion //#region useEnum/index.d.ts /** * 生成双向映射 * * ```ts * const Status = useEnum({ * Pending: 'pending', * Success: 'success', * Error: 'error' * }) * console.log(Status.Pending) // 'pending' * console.log(Status.pending) // 'Pending' * ``` */ declare function useEnum>(obj: T): Expand & { [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]>; }>>; //#endregion export { asyncDebounce, backward, backwardEntries, bindAll, bindSelf, blockThread, clamp, clampArray, collapseWhitespace, getAllKeys, ignoreReject, lerp, mapAtIndex, mapIndexOfKey, nativeHighlight, noop, patternMatching, pipe, sleep, switchLatest, switchLatestWith, toBound, useEnum };