@lordofdestiny/streams-ts

streams-ts

badge

GitHub GitHub Pages

TypeScript implementation of the Streams API for Node.js. Inspired by the Java 8 Stream API and Rust's Iterator trait.

Library is still in development and not yet ready for production use. It's mostly feature complete, but there are still some missing methods and some methods, like cycle and reverse.

Library is written in TypeScript and compiled to ES6. You can use it in both Node.js and the browser, with both TypeScript and JavaScript.

All methods are lazy and return a new Stream object. Even so, focus of the library was more on the API design and less on performance.

Library is heavily tested with Jest.

Examples

import {Stream} from '@lordofdestiny/streams-ts';

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const result = Stream.from(arr)
.filter(x => x % 2 === 0)
.map(x => x * 2)
.toArray();

console.log(result); // [4, 8, 12, 16, 20]

const result2 = Stream.from(arr)
.filter(x => x % 2 === 0)
.map(x => x * 2)
.reduce((acc, x) => acc + x);

console.log(result2); // 60

const result3 = Stream.from(arr)
.filter(x => x % 2 === 0)
.map(x => x * 2)
.findFirst((_) => true);

console.log(result3); // 4

/* We need to make an explicit cast because
TypeScript can't infer the type of the chunked stream
*/
const stream4 = Stream.from(arr).chunk(2) as Stream<[number, number]>;
const result4 = stream4.toMap();

console.log(result4); // Map { 1 => 2, 3 => 4, 5 => 6, 7 => 8, 9 => 10 }

For more examples, check the tests or the documentation.

Installation

This library is hosted on NPM as well as GitHub Packages.

You can install it with: npm i @lordofdestiny/streams-ts

Building

If you decide to clone the repository, you can build the library with:

npm run build

This will generate the build files in the dist directory.

If you want, you can also build the library in watch mode:

npm run build:watch

Running tests

If you decide to clone the repository, you can run tests with:

npm test

Or you can run tests with watch mode:

npm run test:watch

You can also generate coverage report with:

npm run coverage

This opens a browser with coverage report.

Documentation

Official documentation is avilable at here

You can also generate documentation from the source code by using:

npm run docs

After that, you can open docs/index.html in your browser or by running

npm run docs:open

You can check the tests for more examples.

Clean build results

Build results

npm run clean:build

Documentation

npm run clean:docs

Coverage

npm run clean:coverage

Generated using TypeDoc