UNPKG

@mobily/ts-belt

Version:

🔧 Fast, modern, and practical utility library for FP in TypeScript.

276 lines (246 loc) • 8.45 kB
// @flow import type { Result } from "../Result"; import type { Array } from "../types"; import type { Mutable } from "../types"; export type Controlled<A: any[]> = { +cancel: () => void, +invoke: (...args: A) => void, +isScheduled: () => boolean, +schedule: (...args: A) => void, ... }; export type Options = { +delay: number, +leading: boolean, ... }; /** * Always returns the provided value, useful as a placeholder function. */ declare export function identity<A>(value: A): A; /** * Returns `true` if provided values are equal, otherwise, returns `false`. */ declare export function equals<A, B>(snd: A): (fst: B) => boolean; /** * Returns `true` if provided values are equal, otherwise, returns `false`. */ declare export function equals<A, B>(fst: A, snd: B): boolean; declare export function equals<A, B>(snd: B): (fst: A) => boolean; /** * Returns `true` if provided values are equal, otherwise, returns `false`. */ declare export function equals(): boolean; /** * Calls the two provided functions and returns `&&` of the results → `fn0(value) && fn1(value)`. */ declare export function both<A>( value: A, fn0: (_1: A) => boolean, fn1: (_1: A) => boolean ): boolean; declare export function both<A>( fn0: (_1: A) => boolean, fn1: (_1: A) => boolean ): (value: A) => boolean; /** * Calls the two provided functions and returns `||` of the results → `fn0(value) || fn1(value)`. */ declare export function either<A>( value: A, fn0: (_1: A) => boolean, fn1: (_1: A) => boolean ): boolean; declare export function either<A>( fn0: (_1: A) => boolean, fn1: (_1: A) => boolean ): (value: A) => boolean; /** * Returns a function that always returns the provided value. */ declare export function always<A>(value: A): () => A; /** * Returns a default value if `value` is nullable. */ declare export function defaultTo<A, B: $NonMaybeType<A>>( value: A, defaultValue: B ): B; declare export function defaultTo<A, B: $NonMaybeType<A>>( defaultValue: B ): (value: A) => B; /** * Always returns `false`. */ declare export function falsy(): boolean; /** * Always returns `true`. */ declare export function truthy(): boolean; /** * Tests the given value by passing it to the predicate, if the predicate is satisfied, the function returns the result of `truthyFn`, otherwise, returns the result of `falsyFn`. */ declare export function ifElse<A, B>( value: A, predicateFn: (_1: A) => boolean, truthyFn: (_1: A) => B, falsyFn: (_1: A) => B ): B; declare export function ifElse<A, B>( predicateFn: (_1: A) => boolean, truthyFn: (_1: A) => B, falsyFn: (_1: A) => B ): (value: A) => B; /** * Always returns `void`, useful as a placeholder function. */ declare export function ignore(): void; /** * Tests the given value by passing it to the predicate, if the predicate is satisfied, the function returns `value`, otherwise, returns the result of `falsyFn`. */ declare export function unless<A, B>( value: A, predicateFn: (value: A) => boolean, falsyFn: (value: A) => B ): A | B; declare export function unless<A, B>( predicateFn: (value: A) => boolean, falsyFn: (value: A) => B ): (value: A) => A | B; /** * Tests the given value by passing it to the predicate, if the predicate is satisfied, the function returns the result of `truthyFn`, otherwise, returns `value`. */ declare export function when<A, B>( value: A, predicateFn: (value: A) => boolean, truthyFn: (value: A) => B ): A | B; declare export function when<A, B>( predicateFn: (value: A) => boolean, truthyFn: (value: A) => B ): (value: A) => A | B; /** * Determines whether all the provided predicates return `true` for the given value. */ declare export function allPass<A>( value: A, fns: Array<(_1: A) => boolean> ): boolean; declare export function allPass<A>( fns: Array<(_1: A) => boolean> ): (value: A) => boolean; /** * Determines whether at least one of the provided predicate returns `true` for the given value. */ declare export function anyPass<A>( value: A, fns: Array<(_1: A) => boolean> ): boolean; declare export function anyPass<A>( fns: Array<(_1: A) => boolean> ): (value: A) => boolean; /** * Applies a side-effect function on the given value and returns the original value. */ declare export function tap<A>(value: A, fn: (_1: A) => void): A; declare export function tap<A>(fn: (_1: A) => void): (value: A) => A; /** * Takes a function and returns a new function (and other control values) which when used, suppresses calls to the given function to only once within the given `delay`. If `leading` is set to `true`, the function will be allowed to run on the first call before the throttling starts. */ declare export function makeControlledThrottle<A: any[]>( fn: (...args: A) => void, options: Options ): Controlled<A>; declare export function makeControlledThrottle<A: any[]>( options: Options ): (fn: (...args: A) => void) => Controlled<A>; /** * Takes a function and returns a new function (no control values) which when used, suppresses calls to the given function to only once within the given `delay`. */ declare export function throttle<A: any[]>( fn: (...args: A) => void, delay: number ): (...args: A) => void; declare export function throttle<A: any[]>( delay: number ): (fn: (...args: A) => void) => (...args: A) => void; /** * Takes a function, and returns a new function (and other control values) which when called, will only invoke the given input function after a period of inactivity. If `leading` is set to `true`, the function will be invoked immediately. */ declare export function makeControlledDebounce<A: any[]>( fn: (...args: A) => void, options: Options ): Controlled<A>; declare export function makeControlledDebounce<A: any[]>( options: Options ): (fn: (...args: A) => void) => Controlled<A>; /** * Takes a function, and returns a new function (no control values) which when called, will only invoke the given input function after a period of inactivity. */ declare export function debounce<A: any[]>( fn: (...args: A) => void, delay: number ): (...args: A) => void; declare export function debounce<A: any[]>( delay: number ): (fn: (...args: A) => void) => (...args: A) => void; /** * Takes a function, which is called in the `try/catch` block, and returns the `Result` data type. */ declare export function tryCatch<A, B>( value: A, fn: (value: A) => B ): Result<B, string>; declare export function tryCatch<A, B>( fn: (value: A) => B ): (value: A) => Result<B, string>; /** * Takes a function and returns a new function which when called, will allow the first `times` calls to invoke the given function, and any successive calls will be suppressed and the last result will be returned. */ declare export function before<A: any[], B>( times: number, fn: (...args: A) => B ): (...args: A) => B; declare export function before<A: any[], B>( fn: (...args: A) => B ): (times: number) => (...args: A) => B; /** * Takes a function and returns a new function that when called, will suppress the first `times` invocations. */ declare export function after<A: any[], B>( times: number, fn: (...args: A) => B ): (...args: A) => B; declare export function after<A: any[], B>( fn: (...args: A) => B ): (times: number) => (...args: A) => B; /** * Takes a function and returns a new function which will invoke the given function once, and any successive calls will be suppressed, returning the value of the first call. */ declare export function once<A: any[], B>( fn: (...args: A) => B ): (...args: A) => B; /** * Alias for `once`. */ declare export function memoize<A: any[], B>( fn: (...args: A) => B ): (...args: A) => B; /** * Takes a function and returns a new function which once called, stores the result produced by the given function in a closure-based cache, using a cache key created by the function `makeKeyFn`. */ declare export function memoizeWithKey<A: any[], B>( fn: (...args: A) => B ): (makeKeyFn: (...args: A) => string) => (...args: A) => B; declare export function memoizeWithKey<A: any[], B>( makeKeyFn: (...args: A) => string, fn: (...args: A) => B ): (...args: A) => B; /** * Takes a value and converts its immutable type to a mutable one. */ declare export function toMutable<T>(value: T): Mutable<T>; /** * Takes a value and coerces a new type. */ declare export function coerce<T>(value: any): T;