Skip to content

Reusable transformers for WHATWG Streams similar to Rx operators.

License

Notifications You must be signed in to change notification settings

ksm2/stream-transformers

Repository files navigation

stream-transformers

Node.js CI NPM version license TypeScript types

Reusable stream transformers for WHATWG Streams similar to ReactiveX Operators

Table of Contents

Install

Use either

yarn add stream-transformers

or

npm install stream-transformers

Usage

This module allows you to transform WHATWG Streams using different operators.

import { ReadableStream } from "isomorphic-streams";
import { filter, map } from "stream-transformers";

const SECOND = 1000;

const stream = new ReadableStream({
  start(controller) {
    let index = 0;
    setInterval(() => {
      controller.enqueue(index);
      index += 1;
    }, SECOND);
  },
});

const output = stream
  .pipeThrough(map((it) => it * 2))
  .pipeThrough(filter((it) => it < 10));

for await (const value of output) {
  console.dir(value);
}

Operators

  • every - Determines whether every chunk of the stream fulfills a given predicate.
  • filter - Filters chunks emitted by the stream using a predicate.
  • map - Maps each chunk emitted by the stream to another type using a callback.
  • reduce - Aggregates a stream to a value emitting the result when the stream closes.
  • reduce1 - Aggregates a stream to a value emitting the result when the stream closes.
  • scan - Aggregates a stream to a value emitting the intermediate result with each chunk.
  • scan1 - Aggregates a stream to a value emitting the intermediate result with each chunk.
  • some - Determines whether some chunks of the stream fulfill a given predicate.

Contributing

This project is open to feedback and contributions, please open an issue.

stream-transformers follows the Contributor Covenant Code of Conduct.

See Also

Also have a look at the following NPM Packages:

License

MIT © 2021 Konstantin Möllers, see LICENSE.

About

Reusable transformers for WHATWG Streams similar to Rx operators.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published