extra-function
Version:
A function is a set of statements that performs a task or calculates a value.
308 lines (299 loc) • 11 kB
TypeScript
/**
* Do nothing.
* [📘](https://github.com/nodef/extra-function/wiki/NOOP)
* @param args arguments (ignored)
*/
declare function NOOP(...args: any[]): void;
/**
* Return false.
* [📘](https://github.com/nodef/extra-function/wiki/FALSE)
* @param args arguments (ignored)
* @returns false.
*/
declare function FALSE(...args: any[]): false;
/**
* Return true.
* [📘](https://github.com/nodef/extra-function/wiki/TRUE)
* @param args arguments (ignored)
* @returns true.
*/
declare function TRUE(...args: any[]): true;
/**
* Return the same (first) value.
* [📘](https://github.com/nodef/extra-function/wiki/IDENTITY)
* @param v a value
* @returns v
*/
declare function IDENTITY<T>(v: T): T;
/**
* Compare two values.
* [📘](https://github.com/nodef/extra-function/wiki/COMPARE)
* @param a a value
* @param b another value
* @returns a<b: -1, a=b: 0, a>b: 1
*/
declare function COMPARE<T>(a: T, b: T): number;
/**
* Return the arguments passed as a array.
* [📘](https://github.com/nodef/extra-function/wiki/ARGUMENTS)
* @param args arguments
* @returns [...args]
*/
declare function ARGUMENTS(...args: any[]): any[];
/**
* Get the name of a function.
* [📘](https://github.com/nodef/extra-function/wiki/name)
* @param x a function
* @returns name
*/
declare function name(x: Function): string;
/**
* Get the number of parameters of a function.
* [📘](https://github.com/nodef/extra-function/wiki/length)
* @param x a function
* @returns |[p, q, ...]| | x(p, q, ...)
*/
declare function length(x: Function): number;
/**
* Bind this-object, and optional prefix arguments to a function.
* [📘](https://github.com/nodef/extra-function/wiki/bind)
* @param x a function
* @param ths this object to bind
* @param prefix prefix arguments
* @returns (...args) => this.x(...prefix, ...args)
*/
declare function bind(x: Function, ths: any, ...prefix: any[]): Function;
/**
* Invoke a function with specified this-object, and arguments provided individually.
* [📘](https://github.com/nodef/extra-function/wiki/call)
* @param x a function
* @param ths this object to invoke with
* @param args arguments
* @returns this.x(...args)
*/
declare function call(x: Function, ths?: any, ...args: any[]): any;
/**
* Invoke a function with specified this-object, and arguments provided as an array.
* [📘](https://github.com/nodef/extra-function/wiki/apply)
* @param x a function
* @param ths this object to invoke with
* @param args arguments array
* @returns this.x(...args)
*/
declare function apply(x: Function, ths: any, args: any[]): any;
/**
* Check if value is a function.
* [📘](https://github.com/nodef/extra-function/wiki/is)
* @param v a value
* @returns is function?
*/
declare function is(v: any): v is Function;
/**
* Check if value is an async function.
* [📘](https://github.com/nodef/extra-function/wiki/isAsync)
* @param v a value
* @returns is async function?
*/
declare function isAsync(v: any): boolean;
/**
* Check if value is a generator function.
* [📘](https://github.com/nodef/extra-function/wiki/isGenerator)
* @param v a value
* @returns is generator function?
*/
declare function isGenerator(v: any): v is GeneratorFunction;
/**
* Contextify a function by accepting the first parameter as this-object.
* [📘](https://github.com/nodef/extra-function/wiki/contextify)
* @param x a function
* @returns (...args) => x(this, ...args)
*/
declare function contextify(x: Function): Function;
/**
* Decontextify a function by accepting this-object as the first argument.
* [📘](https://github.com/nodef/extra-function/wiki/decontextify)
* @param x a function
* @returns (this, ...args) => this.x(...args)
*/
declare function decontextify(x: Function): Function;
/**
* Generate a result-negated version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/negate)
* @param x a function
* @returns (...args) => !x(...args)
*/
declare function negate(x: Function): Function;
/**
* Resolve arguments into a unique key.
* [📘](https://github.com/nodef/extra-function/wiki/Resolver)
* @param args arguments
* @returns unique key
*/
type Resolver = (...args: any[]) => any;
/**
* Generate result-cached version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/memoize)
* @param x a function
* @param fr resolver ((...args) => unique key) [IDENTITY]
* @param cache result cache [Map()]
*/
declare function memoize(x: Function, fr?: Resolver, cache?: Map<any, any>): Function;
/**
* Generate a parameter-reversed version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/reverse)
* @param x a function
* @returns (p, q, ...) => x(..., q, p)
*/
declare function reverse(x: Function): Function;
/**
* Generate a (first) parameter-spreaded version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/spread)
* @param x a function
* @returns (p, q, ...) => x([p, q, ...])
*/
declare function spread(x: Function): Function;
/**
* Generate a (first) parameter-collapsed version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/unspread)
* @param x a function
* @returns ([p, q, ...]) => x(p, q, ...)
*/
declare function unspread(x: Function): Function;
/**
* Attach prefix arguments to leftmost parameters of a function.
* [📘](https://github.com/nodef/extra-function/wiki/attach)
* @param x a function
* @param prefix prefix arguments
* @returns (...args) => x(...prefix, ...args)
*/
declare function attach(x: Function, ...prefix: any[]): Function;
/**
* Attach suffix arguments to rightmost parameters of a function.
* [📘](https://github.com/nodef/extra-function/wiki/attachRight)
* @param x a function
* @param suffix suffix arguments
* @returns (...args) => x(...args, ...suffix)
*/
declare function attachRight(x: Function, ...suffix: any[]): Function;
/**
* Compose functions together, in applicative order.
* [📘](https://github.com/nodef/extra-function/wiki/compose)
* @param xs functions (f, g)
* @returns (f o g), or f(g(x))
*/
declare function compose(...xs: Function[]): Function;
/**
* Compose functions together, such that result is piped forward.
* [📘](https://github.com/nodef/extra-function/wiki/composeRight)
* @param xs functions (f, g)
* @returns (f ▷ g), or g(f(x))
*/
declare function composeRight(...xs: Function[]): Function;
/**
* Generate curried version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/curry)
* @param x a function
* @param n number of parameters [all]
* @returns (p)(q)(...) => x(p, q, ...)
*/
declare function curry(x: Function, n?: number): Function;
/**
* Generate right-curried version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/curryRight)
* @param x a function
* @param n number of parameters [all]
* @returns (p)(q)(...) => x(..., q, p)
*/
declare function curryRight(x: Function, n?: number): Function;
/**
* Invocation control for time/rate-controlled functions.
* [📘](https://github.com/nodef/extra-function/wiki/InvocationControl)
*/
interface InvocationControl {
/** Disable invoking of target function. */
clear: () => void;
/** Immediately invoke target function. */
flush: () => void;
}
/**
* Generate deferred version of a function, that executes after the current stack has cleared.
* [📘](https://github.com/nodef/extra-function/wiki/defer)
* @param x a function
* @returns (...args) => invocation control
*/
declare function defer(x: Function): Function;
/**
* Generate delayed version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/delay)
* @param x a function
* @param t delay time (ms)
* @returns (...args) => invocation control
*/
declare function delay(x: Function, t: number): Function;
/**
* Generate restricted-use version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/restrict)
* @param x a function
* @param start usable from
* @param end usable till (excluding) [-1 ⇒ end]
* @returns (...args) => x(...args) from [start:end] calls
*/
declare function restrict(x: Function, start: number, end?: number): Function;
/**
* Restrict a function to be used only once.
* [📘](https://github.com/nodef/extra-function/wiki/restrictOnce)
* @param x a function
* @returns (...args) => x(...args) from [0:1] calls
*/
declare function restrictOnce(x: Function): Function;
/**
* Restrict a function to be used only upto a certain number of calls.
* [📘](https://github.com/nodef/extra-function/wiki/restrictBefore)
* @param x a function
* @param n number of calls upto which it is usable
* @returns (...args) => x(...args) from [0:n] calls
*/
declare function restrictBefore(x: Function, n: number): Function;
/**
* Restrict a function to be used only after a certain number of calls.
* [📘](https://github.com/nodef/extra-function/wiki/restrictAfter)
* @param x a function
* @param n number of calls after which it is usable
* @returns (...args) => x(...args) from [n:end] calls
*/
declare function restrictAfter(x: Function, n: number): Function;
/**
* Generate debounced version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/debounce)
* @param x a function
* @param t delay time (ms)
* @param T max delay time [-1 ⇒ none]
* @returns (...args) => invocation control
*/
declare function debounce(x: Function, t: number, T?: number): Function;
/**
* Generate leading-edge debounced version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/debounceEarly)
* @param x a function
* @param t delay time (ms)
* @param T max delay time [-1 ⇒ none]
* @returns (...args) => invocation control
*/
declare function debounceEarly(x: Function, t: number, T?: number): Function;
/**
* Generate throttled version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/throttle)
* @param x a function
* @param t wait time (ms)
* @returns (...args) => invocation control
*/
declare function throttle(x: Function, t: number): Function;
/**
* Generate leading-edge throttled version of a function.
* [📘](https://github.com/nodef/extra-function/wiki/throttleEarly)
* @param x a function
* @param t wait time (ms)
* @returns (...args) => invocation control
*/
declare function throttleEarly(x: Function, t: number): Function;
export { ARGUMENTS, COMPARE, FALSE, IDENTITY, type InvocationControl, NOOP, type Resolver, TRUE, restrictAfter as after, apply, length as arity, attach, attachRight, restrictBefore as before, bind, call, compose, composeRight, contextify, curry, curryRight, debounce, debounceEarly, decontextify, defer, delay, reverse as flip, is, isAsync, isGenerator, length, memoize, name, negate, restrictOnce as once, attach as partial, attachRight as partialRight, restrict, restrictAfter, restrictBefore, restrictOnce, reverse, spread, throttle, throttleEarly, unspread };