README
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
})