core3-utils

@wixc3/common on Github

Home > @wixc3/common > chain

chain() function

Chain iterable operations, each acting on the output of the previous step

Signature:

export declare function chain<T, V extends NotIterable<T>>(value: V): ValueChain<V>;

Parameters

Parameter Type Description
value V initial value

Returns:

ValueChain<V>

Chainable action on iterable

Example 1

When the action is per item, the result is accessible as *iterable*

chain([0,1,2])
     .filter(i => i)
     .map(i => i**2)
     .iterable
// => [1,4]

Example 2

When the action returns an element (as in first, next, reduce etc) the the result is accessible as *value*

chain("hello").map(i => i.split("")).first().value
// => "h"

Example 3

Iterable is always accessible, as a single element iterable

chain([0,1,2]).filter(i => i).first().iterable
// => [1]

Example 4

Note if the action returned undefined, iterable will be empty

chain([]).first().iterable // => []
chain([]).first().value // => undefined