Skip to main content

Function: chain()

chain<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R>(initial, fn0, fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, fn12, fn13, fn14, fn15): R

Applies a series of functions to an initial value, passing the result of each function to the next.

Used to convert code that looks like:

const a = addB(addC({ a: 'a' }));

to:

const a = chain({ a: 'a' }, addB, addC);

See composable-options for an example of how this can be used.

NOTE: Seeing a return type of unknown? This is likely due to the number of functions passed to chain exceeding the number of overloads provided. There is a technical limitation in TypeScript that means we need to provide a finite number of overloads. While we anticipate that 16 overloads should be enough for most use cases, if you need more you can combine chains together like below:

const a = chain(
{ a: 'a' },
addB,
(iv) => chain(iv, addC, addD, addE),
);

Type Parameters

T0

T1

T2

T3

T4

T5

T6

T7

T8

T9

T10

T11

T12

T13

T14

T15

R

Parameters

initial: T0

Initial value to pass through the chain

fn0: UnaryFunction<T0, T1>

fn1: UnaryFunction<T1, T2>

fn2: UnaryFunction<T2, T3>

fn3: UnaryFunction<T3, T4>

fn4: UnaryFunction<T4, T5>

fn5: UnaryFunction<T5, T6>

fn6: UnaryFunction<T6, T7>

fn7: UnaryFunction<T7, T8>

fn8: UnaryFunction<T8, T9>

fn9: UnaryFunction<T9, T10>

fn10: UnaryFunction<T10, T11>

fn11: UnaryFunction<T11, T12>

fn12: UnaryFunction<T12, T13>

fn13: UnaryFunction<T13, T14>

fn14: UnaryFunction<T14, T15>

fn15: UnaryFunction<T15, R>

Returns

R

Updated value after all functions have been applied

Updated value after all functions have been applied

Param

Initial value to pass through the chain

Param

Functions to apply to the initial value, and each subsequent value

Defined in

packages/parser/src/lib/utils/chain.ts:526