Home > @wixc3/common > chain
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>;
Parameter | Type | Description |
---|---|---|
value | V | initial value |
Returns:
ValueChain<V>
Chainable action on iterable
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]
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"
Iterable is always accessible, as a single element iterable
chain([0,1,2]).filter(i => i).first().iterable
// => [1]
Note if the action returned undefined, iterable will be empty
chain([]).first().iterable // => []
chain([]).first().value // => undefined