/** * Promise, or maybe not */ type Awaitable = T | PromiseLike; /** * Null or whatever */ type Nullable = T | null | undefined; /** * Array, or not yet */ type Arrayable = T | Array; /** * Function */ type Fn = () => T; /** * Constructor */ type Constructor = new (...args: any[]) => T; /** * Infers the element type of an array */ type ElementOf = T extends (infer E)[] ? E : never; /** * Defines an intersection type of all union items. * * @param U Union of any types that will be intersected. * @returns U items intersected * @see https://stackoverflow.com/a/50375286/9259330 */ type UnionToIntersection = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; /** * Infers the arguments type of a function */ type ArgumentsType = T extends ((...args: infer A) => any) ? A : never; type MergeInsertions = T extends object ? { [K in keyof T]: MergeInsertions; } : T; type DeepMerge = MergeInsertions<{ [K in keyof F | keyof S]: K extends keyof S & keyof F ? DeepMerge : K extends keyof S ? S[K] : K extends keyof F ? F[K] : never; }>; /** * Convert `Arrayable` to `Array` * * @category Array */ declare function toArray(array?: Nullable>): Array; /** * Convert `Arrayable` to `Array` and flatten it * * @category Array */ declare function flattenArrayable(array?: Nullable>>): Array; /** * Use rest arguments to merge arrays * * @category Array */ declare function mergeArrayable(...args: Nullable>[]): Array; type PartitionFilter = (i: T, idx: number, arr: readonly T[]) => any; /** * Divide an array into two parts by a filter function * * @category Array * @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0) */ declare function partition(array: readonly T[], f1: PartitionFilter): [T[], T[]]; declare function partition(array: readonly T[], f1: PartitionFilter, f2: PartitionFilter): [T[], T[], T[]]; declare function partition(array: readonly T[], f1: PartitionFilter, f2: PartitionFilter, f3: PartitionFilter): [T[], T[], T[], T[]]; declare function partition(array: readonly T[], f1: PartitionFilter, f2: PartitionFilter, f3: PartitionFilter, f4: PartitionFilter): [T[], T[], T[], T[], T[]]; declare function partition(array: readonly T[], f1: PartitionFilter, f2: PartitionFilter, f3: PartitionFilter, f4: PartitionFilter, f5: PartitionFilter): [T[], T[], T[], T[], T[], T[]]; declare function partition(array: readonly T[], f1: PartitionFilter, f2: PartitionFilter, f3: PartitionFilter, f4: PartitionFilter, f5: PartitionFilter, f6: PartitionFilter): [T[], T[], T[], T[], T[], T[], T[]]; /** * Unique an Array * * @category Array */ declare function uniq(array: readonly T[]): T[]; /** * Unique an Array by a custom equality function * * @category Array */ declare function uniqueBy(array: readonly T[], equalFn: (a: any, b: any) => boolean): T[]; /** * Get last item * * @category Array */ declare function last(array: readonly []): undefined; declare function last(array: readonly T[]): T; /** * Remove an item from Array * * @category Array */ declare function remove(array: T[], value: T): boolean; /** * Get nth item of Array. Negative for backward * * @category Array */ declare function at(array: readonly [], index: number): undefined; declare function at(array: readonly T[], index: number): T; /** * Genrate a range array of numbers. The `stop` is exclusive. * * @category Array */ declare function range(stop: number): number[]; declare function range(start: number, stop: number, step?: number): number[]; /** * Move element in an Array * * @category Array * @param arr * @param from * @param to */ declare function move(arr: T[], from: number, to: number): T[]; /** * Clamp a number to the index ranage of an array * * @category Array */ declare function clampArrayRange(n: number, arr: readonly unknown[]): number; /** * Get random items from an array * * @category Array */ declare function sample(arr: T[], count: number): T[]; /** * Shuffle an array. This function mutates the array. * * @category Array */ declare function shuffle(array: T[]): T[]; declare const assert: (condition: boolean, message: string) => asserts condition; declare const toString: (v: any) => string; declare const getTypeName: (v: any) => string; declare const noop: () => void; /** * Type guard to filter out null-ish values * * @category Guards * @example array.filter(notNullish) */ declare function notNullish(v: T | null | undefined): v is NonNullable; /** * Type guard to filter out null values * * @category Guards * @example array.filter(noNull) */ declare function noNull(v: T | null): v is Exclude; /** * Type guard to filter out null-ish values * * @category Guards * @example array.filter(notUndefined) */ declare function notUndefined(v: T): v is Exclude; /** * Type guard to filter out falsy values * * @category Guards * @example array.filter(isTruthy) */ declare function isTruthy(v: T): v is NonNullable; declare const isDef: (val?: T | undefined) => val is T; declare const isBoolean: (val: any) => val is boolean; declare const isFunction: (val: any) => val is T; declare const isNumber: (val: any) => val is number; declare const isString: (val: unknown) => val is string; declare const isObject: (val: any) => val is object; declare const isUndefined: (val: any) => val is undefined; declare const isNull: (val: any) => val is null; declare const isRegExp: (val: any) => val is RegExp; declare const isDate: (val: any) => val is Date; declare const isWindow: (val: any) => boolean; declare const isBrowser: boolean; declare function clamp(n: number, min: number, max: number): number; declare function sum(...args: number[] | number[][]): number; /** * Replace backslash to slash * * @category String */ declare function slash(str: string): string; /** * Ensure prefix of a string * * @category String */ declare function ensurePrefix(prefix: string, str: string): string; /** * Ensure suffix of a string * * @category String */ declare function ensureSuffix(suffix: string, str: string): string; /** * Dead simple template engine, just like Python's `.format()` * * @category String * @example * ``` * const result = template( * 'Hello {0}! My name is {1}.', * 'Inès', * 'Anthony' * ) // Hello Inès! My name is Anthony. * ``` */ declare function template(str: string, ...args: any[]): string; /** * Generate a random string * @category String */ declare function randomStr(size?: number, dict?: string): string; /** * First letter uppercase, other lowercase * @category string * @example * ``` * capitalize('hello') => 'Hello' * ``` */ declare function capitalize(str: string): string; declare const timestamp: () => number; /** * Call every function in an array */ declare function batchInvoke(functions: Nullable[]): void; /** * Call the function */ declare function invoke(fn: Fn): void; /** * Pass the value through the callback, and return the value * * @example * ``` * function createUser(name: string): User { * return tap(new User, user => { * user.name = name * }) * } * ``` */ declare function tap(value: T, callback: (value: T) => void): T; /** * Map key/value pairs for an object, and construct a new one * * * @category Object * * Transform: * @example * ``` * objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()]) * // { A: '1', B: '2' } * ``` * * Swap key/value: * @example * ``` * objectMap({ a: 1, b: 2 }, (k, v) => [v, k]) * // { 1: 'a', 2: 'b' } * ``` * * Filter keys: * @example * ``` * objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v]) * // { b: 2 } * ``` */ declare function objectMap(obj: Record, fn: (key: K, value: V) => [NK, NV] | undefined): Record; /** * Type guard for any key, `k`. * Marks `k` as a key of `T` if `k` is in `obj`. * * @category Object * @param obj object to query for key `k` * @param k key to check existence in `obj` */ declare function isKeyOf(obj: T, k: keyof any): k is keyof T; /** * Strict typed `Object.keys` * * @category Object */ declare function objectKeys(obj: T): (`${keyof T & undefined}` | `${keyof T & null}` | `${keyof T & string}` | `${keyof T & number}` | `${keyof T & false}` | `${keyof T & true}`)[]; /** * Strict typed `Object.entries` * * @category Object */ declare function objectEntries(obj: T): [keyof T, T[keyof T]][]; /** * Deep merge :P * * @category Object */ declare function deepMerge(target: T, ...sources: S[]): DeepMerge; /** * Create a new subset object by giving keys * * @category Object */ declare function objectPick(obj: O, keys: T[], omitUndefined?: boolean): Pick; /** * Clear undefined fields from an object. It mutates the object * * @category Object */ declare function clearUndefined(obj: T): T; /** * Determines whether an object has a property with the specified name * * @see https://eslint.org/docs/rules/no-prototype-builtins * @category Object */ declare function hasOwnProperty(obj: T, v: PropertyKey): boolean; interface SingletonPromiseReturn { (): Promise; /** * Reset current staled promise. * Await it to have proper shutdown. */ reset: () => Promise; } /** * Create singleton promise function * * @category Promise */ declare function createSingletonPromise(fn: () => Promise): SingletonPromiseReturn; /** * Promised `setTimeout` * * @category Promise */ declare function sleep(ms: number, callback?: Fn): Promise; /** * Create a promise lock * * @category Promise * @example * ``` * const lock = createPromiseLock() * * lock.run(async () => { * await doSomething() * }) * * // in anther context: * await lock.wait() // it will wait all tasking finished * ``` */ declare function createPromiseLock(): { run(fn: () => Promise): Promise; wait(): Promise; isWaiting(): boolean; clear(): void; }; /** * Promise with `resolve` and `reject` methods of itself */ interface ControlledPromise extends Promise { resolve(value: T | PromiseLike): void; reject(reason?: any): void; } /** * Return a Promise with `resolve` and `reject` methods * * @category Promise * @example * ``` * const promise = createControlledPromise() * * await promise * * // in anther context: * promise.resolve(data) * ``` */ declare function createControlledPromise(): ControlledPromise; // Type definitions for throttle-debounce 5.0 interface CancelOptions { upcomingOnly?: boolean; } interface Cancel { cancel: (options?: CancelOptions) => void; } interface NoReturn any> { (...args: Parameters): void; } interface ThrottleOptions { noTrailing?: boolean; noLeading?: boolean; debounceMode?: boolean; } interface DebounceOptions { atBegin?: boolean; } type throttle any> = NoReturn & Cancel; /** * Throttle execution of a function. Especially useful for rate limiting * execution of handlers on events like resize and scroll. * * @param delay * A zero-or-greater delay in milliseconds. For event callbacks, values around * 100 or 250 (or even higher) are most useful. * * @param callback * A function to be executed after delay milliseconds. The `this` context and * all arguments are passed through, as-is, to `callback` when the * throttled-function is executed. * * @param options * An object to configure options. * * @param options.noTrailing * Optional, defaults to false. If noTrailing is true, callback will only execute * every `delay` milliseconds while the throttled-function is being called. If * noTrailing is false or unspecified, callback will be executed one final time * after the last throttled-function call. (After the throttled-function has not * been called for `delay` milliseconds, the internal counter is reset) * * @param options.noLeading * Optional, defaults to false. If noLeading is false, the first throttled-function * call will execute callback immediately. If noLeading is true, the first the * callback execution will be skipped. It should be noted that callback will never * executed if both noLeading = true and noTrailing = true. * * @param options.debounceMode If `debounceMode` is true (at begin), schedule * `callback` to execute after `delay` ms. If `debounceMode` is false (at end), * schedule `callback` to execute after `delay` ms. * * @return * A new, throttled, function. */ declare function throttle any>( delay: number, callback: T, options?: ThrottleOptions, ): throttle; type debounce any> = NoReturn & Cancel; /** * Debounce execution of a function. Debouncing, unlike throttling, * guarantees that a function is only executed a single time, either at the * very beginning of a series of calls, or at the very end. * * @param delay * A zero-or-greater delay in milliseconds. For event callbacks, values around * 100 or 250 (or even higher) are most useful. * * @param callback * A function to be executed after delay milliseconds. The `this` context and * all arguments are passed through, as-is, to `callback` when the * debounced-function is executed. * * @param options * An object to configure options. * * @param options.atBegin * If atBegin is false or unspecified, callback will only be executed `delay` * milliseconds after the last debounced-function call. If atBegin is true, * callback will be executed only at the first debounced-function call. (After * the throttled-function has not been called for `delay` milliseconds, the * internal counter is reset). * * @return * A new, debounced function. */ declare function debounce any>( delay: number, callback: T, options?: DebounceOptions, ): debounce; interface POptions { /** * How many promises are resolved at the same time. */ concurrency?: number | undefined; } declare class PInstance extends Promise[]> { items: Iterable; options?: POptions | undefined; private promises; get promise(): Promise[]>; constructor(items?: Iterable, options?: POptions | undefined); add(...args: (T | Promise)[]): void; map(fn: (value: Awaited, index: number) => U): PInstance>; filter(fn: (value: Awaited, index: number) => boolean | Promise): PInstance>; forEach(fn: (value: Awaited, index: number) => void): Promise; reduce(fn: (previousValue: U, currentValue: Awaited, currentIndex: number, array: Awaited[]) => U, initialValue: U): Promise; clear(): void; then(fn?: () => PromiseLike): Promise; catch(fn?: (err: unknown) => PromiseLike): Promise; finally(fn?: () => void): Promise[]>; } /** * Utility for managing multiple promises. * * @see https://github.com/antfu/utils/tree/main/docs/p.md * @category Promise * @example * ``` * import { p } from '@antfu/utils' * * const items = [1, 2, 3, 4, 5] * * await p(items) * .map(async i => await multiply(i, 3)) * .filter(async i => await isEven(i)) * // [6, 12] * ``` */ declare function p(items?: Iterable, options?: POptions): PInstance; export { ArgumentsType, Arrayable, Awaitable, Constructor, ControlledPromise, DeepMerge, ElementOf, Fn, MergeInsertions, Nullable, PartitionFilter, SingletonPromiseReturn, UnionToIntersection, assert, at, batchInvoke, capitalize, clamp, clampArrayRange, clearUndefined, createControlledPromise, createPromiseLock, createSingletonPromise, debounce, deepMerge, ensurePrefix, ensureSuffix, flattenArrayable, getTypeName, hasOwnProperty, invoke, isBoolean, isBrowser, isDate, isDef, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isString, isTruthy, isUndefined, isWindow, last, mergeArrayable, move, noNull, noop, notNullish, notUndefined, objectEntries, objectKeys, objectMap, objectPick, p, partition, randomStr, range, remove, sample, shuffle, slash, sleep, sum, tap, template, throttle, timestamp, toArray, toString, uniq, uniqueBy };