import { AsyncOrSync } from 'ts-essentials'; /** Runs a function if the argument is not null or undefined. */ export declare function ifPresent(val: V | null | undefined, fn: (v: Exclude) => W): W | undefined; /** * Wraps a function, returning a function which will call the handler at most * once. Subsequent calls are logged but do not trigger the handler. */ export declare function atMostOnce(fn: (arg: V) => void, cb?: (arg: V, attempt: number) => void): AtMostOnce; export interface AtMostOnce { (arg: V): void; readonly callCount: number; } /** * Creates a promise which can be resolved by calling the returned callback. * This is similar to "deferred" objects provided by other libraries. The * optional callback argument will be called just before the promise is * resolved (or rejected). */ export declare function resolvable(cb?: ResolvableCallback): [Promise, ResolvableCallback]; /** Function used to resolve a deferred returned by `resolvable`. */ export type ResolvableCallback = (err?: unknown, val?: V) => void; /** Placeholder sync function. */ export declare function noop(): void; /** Placeholder async function. */ export declare function pass(): Promise; /** Async side-effect convenience type. */ export type Effect = () => AsyncOrSync; /** Identity function. */ export declare function identity(val: V): V; /** Endomorphism type. */ export type Endo = (val: V) => V; /** * Transforms a single-argument-string function, for example an opaque factory * method into a function suitable for use as a tag function in template * strings. */ export declare function asTagFunction(fn: (val: string) => V): (vals: ReadonlyArray) => V; /** Common function callback interface. */ export interface Callback { (err?: Error, val?: undefined): void; (err: null | undefined, val: V): void; } /** * Returns a consumer function which collects its arguments into an array, * accessible via the `collected` property. This is useful for example to * collect shutdown callbacks in a decentralized manner and easily run them * later on. */ export declare function collectable(): Collectable; /** Recording consumer. */ export type Collectable = Collect & { readonly collected: V[]; }; /** Synchronous cleanup effect. */ export type Cleanup = () => void; /** * Adds a argument to the collectable. The returned cleanup function can be * callled to remove the argument if it is still present. */ export type Collect = (arg: V) => Cleanup; /** * Returns a comparator which accepts nulls and sorts them according to the * first argument. Note that `Array.prototype.sort` always sorts `undefined` * last, even when a custom comparator is used; use `null`s if this is an issue. */ export declare function sortingNulls(pos: 'first' | 'last', comp: (v1: V, v2: V) => number): (v1: V | null | undefined, v2: V | null | undefined) => number; /** * Returns functions wrapping a counter as internal state. This is useful for * example in tests to generate unique values deterministically: The first * `seqno` argument is 1. * * const freshEmail = incrementing((seqno) => `u${seqno}@test.opvious.io`); */ export declare function incrementing(fn: (seqno: number) => V): () => V; /** * Calls a method on an object with the trailing inputs as arguments. This is * equivalent to `obj[name](...args)` but supports calling methods which are a * union of function types. */ export declare function methodCall V>(obj: O, name: N, ...args: O[N] extends F ? A : O[N] extends (...args: any) => any ? Parameters : never): V;