Skip to content

Generator

Functions to manipulate JavaScript generators in a functional and type-safe way. Generators allow you to process data sequences lazily, computing values only when they are needed.

How to import?

The library exposes the DGenerator and G namespaces from the main entry or via direct import (tree-shaking friendly), which lets you only load what you need.

typescript
import { DGenerator, G } from "@duplojs/utils";
import * as DGenerator from "@duplojs/utils/generator";
import * as G from "@duplojs/utils/generator";

Execution

execute

Executes a generator and returns all its values in an array.

Iteration

loop

Iterates over each element of a generator with a callback function.

asyncLoop

Iterates over each element of a generator with an async callback function.

Transformation

map

Transforms each element of a generator by applying a function.

asyncMap

Transforms each element of a generator with an async function.

chunk

Splits an iterable into fixed-size chunks and returns a generator of arrays.

Filtering

filter

Filters the elements of a generator according to a predicate.

asyncFilter

Filters the elements of a generator with an async predicate.

Reduction

reduce

Reduces a generator to a single value by applying an accumulator function.

asyncReduce

Reduces a generator to a single value with an async accumulator function.

Released under the MIT license.