UNPKG

@resolid/utils

Version:
144 lines (135 loc) 4.98 kB
declare const asArray: <T>(value: T | T[]) => T[]; type Booleanish = boolean | "true" | "false"; type Simplify<T> = { [K in keyof T]: T[K]; } & {}; type ValueOrFunction<T, A extends unknown[] = []> = T | ((...args: A) => T); /** * 空函数,什么也不做。 */ declare const noop: () => void; /** * 使程序在指定的毫秒数内暂停。 */ declare const sleep: (ms: number) => Promise<unknown>; /** * 返回一个始终返回相同值的函数。 */ declare const always: <T>(value: T) => (() => T); /** * 统一处理成功和失败的返回结果。 */ declare const to: <T, E = Error>(promise: Promise<T>) => Promise<[E, undefined] | [null, T]>; /** * 如果传入的是函数,则调用它并返回结果。如果不是函数,则直接返回传入的值。 */ declare function runIf<T, A extends unknown[]>(value: ValueOrFunction<T, A>, ...args: A): T; declare const __DEV__: boolean; declare const isBrowser: boolean; /** * 检查给定的值是否为布尔值。 */ declare const isBoolean: (value: unknown) => value is boolean; /** * 检查给定的值是否为数字。 */ declare const isNumber: (value: unknown) => value is number; /** * 检查给定的值是否为字符串。 */ declare const isString: (value: unknown) => value is string; /** * 检查给定的值是否为对象。 */ declare const isObject: (value: unknown) => value is Record<string, unknown>; /** * 检查给定的值是否为 `undefined`。 */ declare const isUndefined: (value: unknown) => value is undefined; /** * 检查给定的值是否为 `null` 或 `undefined`。 */ declare const isNullish: (value: unknown) => value is null | undefined; /** * 检查给定的值是否为函数。 */ declare const isFunction: (value: unknown) => value is Function; /** * 检查给定的值是否为日期类型 */ declare const isDate: (value: unknown) => value is Date; /** * 检查给定的值是否为空值。 */ declare const isEmpty: (value: unknown) => boolean; /** * 检查给定的值是否为 `Promise`。 */ declare const isPromise: (value: unknown) => value is Promise<unknown>; /** * 将数值限制在指定的最小值和最大值之间。 * * @param {number} value - 需要进行限制的数值。 * @param {[number, number]} range - 包含最小值和最大值的元组 [min, max]。 * @returns {number} - 限制后的数值。 */ declare const clamp: (value: number, [min, max]: [number, number]) => number; /** * 生成一个指定范围的数字数组。 */ declare const range: (start: number, end: number) => number[]; /** * 从对象中省略指定的键,返回一个新的对象,不影响原始对象。 */ declare const omit: <T extends object, K extends keyof T>(object: T, keys: K[]) => Omit<T, K>; /** * 将字符串的首字母大写,其余部分保持不变。 */ declare const capitalize: (str: string) => string; /** * 检查字符串是否以指定前缀开头,可选择忽略大小写。 */ declare const startsWith: (text: string, prefix: string, ignoreCase?: boolean) => boolean; /** * 检查字符串是否以指定后缀结尾,可选择忽略大小写。 */ declare const endsWith: (text: string, suffix: string, ignoreCase?: boolean) => boolean; declare const trimStart: (text: string, prefix: string, ignoreCase?: boolean) => string; declare const trimEnd: (text: string, suffix: string, ignoreCase?: boolean) => string; type ThrottleOptions = { /** * 在第一次调用时立即执行 */ start?: boolean; /** * 当等待时间(wait)结束后立即执行 */ middle?: boolean; /** * 在第一次成功调用后取消 */ once?: boolean; }; type Throttler<T extends unknown[]> = { (...args: T): void; cancel: () => void; }; /** * 节流函数,用于限制函数的执行频率。 * * @param callback - 需要节流的回调函数 * @param wait - 等待时间(毫秒) * @param options - 配置选项 * @returns - 返回一个带有 `cancel` 方法的节流函数 */ declare const throttle: <T extends unknown[]>(callback: (...args: T) => unknown, wait?: number, { start, middle, once }?: ThrottleOptions) => Throttler<T>; /** * 防抖函数,执行延迟后的回调,避免频繁触发。 * * @param callback - 需要防抖的回调函数 * @param wait - 等待时间(毫秒) * @param options - 配置选项 * @returns - 返回一个带有 `cancel` 方法的防抖函数 */ declare const debounce: <T extends unknown[]>(callback: (...args: T) => unknown, wait?: number, { start, middle, once }?: ThrottleOptions) => Throttler<T>; export { type Booleanish, type Simplify, type ThrottleOptions, type Throttler, type ValueOrFunction, __DEV__, always, asArray, capitalize, clamp, debounce, endsWith, isBoolean, isBrowser, isDate, isEmpty, isFunction, isNullish, isNumber, isObject, isPromise, isString, isUndefined, noop, omit, range, runIf, sleep, startsWith, throttle, to, trimEnd, trimStart };