Skip to main content

Chain

Defined in: src/Chain.ts:22

A chain of operators over an Iter, created with chain. Every method that applies an operator returns a new chain; the chain is an AsyncIterable.

Type Parameters​

I​

I

Implements​

  • AsyncIterable<I>

Methods​

[asyncIterator]()​

[asyncIterator](): AsyncGenerator<Awaited<I>, void, any>

Defined in: src/Chain.ts:25

Returns​

AsyncGenerator<Awaited<I>, void, any>

Implementation of​

AsyncIterable.[asyncIterator]


batch()​

batch(options): Chain<I[]>

Defined in: src/Chain.ts:140

Batches items to the given size.

The resulting chain will be a chain of arrays of the given size maximum. Once the iteration is stopped, the rest of the items will be returned as a batch of possibly smaller size.

Parameters​

options​

number | BatchOptions

Returns​

Chain<I[]>

See​

batch


bufferize()​

bufferize<O>(options): Chain<O>

Defined in: src/Chain.ts:209

Accumulates items into a value and emits it when shouldFlush returns true, when timeFrame elapses, or when the source ends.

Type Parameters​

O​

O

Parameters​

options​

BufferizeOptions<I, O>

Returns​

Chain<O>

See​

bufferize


concurrentMap()​

concurrentMap<Output, ErrorOutput>(options, mapper, errorMapper?): Chain<Output | ErrorOutput>

Defined in: src/Chain.ts:113

The same as map, but allowing to process values in parallel

Type Parameters​

Output​

Output

ErrorOutput​

ErrorOutput = never

Parameters​

options​

ConcurrentMapOptions

mapper​

(input) => Output | Promise<Output>

errorMapper?​

(input, error) => ErrorOutput | Promise<ErrorOutput>

Returns​

Chain<Output | ErrorOutput>

See​

concurrentMap


consume()​

consume(callback?): Promise<void>

Defined in: src/Chain.ts:74

Starts the iteration. Resolves when the iteration is done.

Optionally, a callback can be provided. It will be called for each item in the iteration.

Parameters​

callback?​

(value) => void | Promise<void>

Returns​

Promise<void>

Example​

const seen: number[] = [];
await chain([1, 2, 3]).consume((n) => {
seen.push(n);
});
// seen => [1, 2, 3]

filter()​

filter<Output>(predicate): Chain<Output>

Defined in: src/Chain.ts:174

Filters items from the chain.

Unlike the most of chain methods, the filter's predicate must be a synchronous function, because it must return a type predicate.

Type Parameters​

Output​

Output

Parameters​

predicate​

(value) => value is Output

Returns​

Chain<Output>

See​

filter


flatten()​

flatten(): I extends unknown[] ? Chain<I[number]> : never

Defined in: src/Chain.ts:160

For the chains of arrays, returns a new chain of those arrays' items. For the chains on non-arrays, does not compile.

Returns​

I extends unknown[] ? Chain<I[number]> : never

See​

flatten


interval()​

interval(size): Chain<[I, I]>

Defined in: src/Chain.ts:151

Calculates the intervals of the items.

Works like batch, but instead of returning batches of the given size, it returns pairs of their first and last items.

Parameters​

size​

number

Returns​

Chain<[I, I]>

See​

interval


map()​

map<Output, ErrorOutput>(mapper, errorMapper?): Chain<Output | ErrorOutput>

Defined in: src/Chain.ts:99

Maps the values.

Optionally, a errorMapper can be provided which allows to handle errors thrown by the mapper and return a error value.

Type Parameters​

Output​

Output

ErrorOutput​

ErrorOutput = never

Parameters​

mapper​

(input) => Output | Promise<Output>

errorMapper?​

(input, error) => ErrorOutput | Promise<ErrorOutput>

Returns​

Chain<Output | ErrorOutput>

See​

map


onEnd()​

onEnd(cb): Chain<I>

Defined in: src/Chain.ts:219

Called once, when the iteration is done. It is not called if the consumer stops early or the source throws.

Parameters​

cb​

() => void

Returns​

Chain<I>

See​

onEnd


pipe()​

pipe<O>(op): Chain<O>

Defined in: src/Chain.ts:52

Pipes the values through an operator

An operator is a function receiving one Iter and returning another Iter.

Also, for convenience, an operator can be an object with a process method, which is an operator function itself.

Type Parameters​

O​

O

Parameters​

op​

Operator<I, O>

Returns​

Chain<O>

Example​

function double(source: Iter<number>): Iter<number> {
return (async function* () {
for await (const n of source) {
yield n * 2;
}
})();
}

await chain([1, 2]).pipe(double).toArray(); // => [2, 4]
await chain([1, 2]).pipe({ process: double }).toArray(); // => [2, 4]

skip()​

skip(size): Chain<I>

Defined in: src/Chain.ts:199

Drops the first size items.

Parameters​

size​

number

Returns​

Chain<I>

See​

skip


take()​

take(size): Chain<I>

Defined in: src/Chain.ts:190

Passes through only the first size items and stops iterating the source.

Parameters​

size​

number

Returns​

Chain<I>

See​

take


tap()​

tap(tapper): Chain<I>

Defined in: src/Chain.ts:128

Calls a function for each item without changing items in the chain.

Parameters​

tapper​

(input) => void | Promise<void>

Returns​

Chain<I>

See​

tap


toArray()​

toArray(): Promise<I[]>

Defined in: src/Chain.ts:83

When the iteration is done, resolves to an array of all the items iterated over

Returns​

Promise<I[]>