1.0.0 • Published 2 years ago

stream-transformers v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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 test WHATWG Streams with Marble Syntax in Jest.

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.