1.0.4 • Published 5 years ago

node.ts-streams v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Node.ts-Streams

Simple and lightweight wrapper for Node streams in Typescript.

Correctness and proper typescript typings.

Install

    npm i node.ts-streams

How to start

Instanciate the first instance of Stream with an Array or a Readable Stream (ie: from database)

    // Stream type but be specified if with a Readable stream
    const stream = new Stream<{ id: number }>(readableStream)
    // Stream type is inferred with an Array
    const stream = Stream.fromArray(array)

Chain transformations and operations

Most transformations return an instance of Stream to allow chainning

Map to transform data

Synchronous:

    stream
        .map((input) => {
            return {
                informations: input,
                date: Date.now()
            }
        })

Asynchronous with a promise:

    stream
        .map((input) => {
            return db.find(input).then((data) => ({ input, data })
        })

Filter to clean data

Always synchronous

    stream
        .map((input) => {
            return db.find(input).then((data) => ({ input, data })
        })
        .filter((e) => e.data.errors.length === 0)

Reduce to compute data

    stream
        .reduce((acc, curr) => {
            return acc + curr
        }, 0)

Add a writing function to end the stream

    stream
        addWritingStream((chunk, encoding, next) => {
            // use the chunk of data here and call next after that
        })

Use to Array to return in a standard Promise / array context

    stream
        .toArray()
        .then((streamAsArray) => {
            // use array here
        })
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago