Enumeration Stream
Add enumeration methods (like map, filter, forEach, reduce) to streams!
npm install -S enum-stream
Example
const EnumStream = ; EnumStream // Synchronous iterator // Synchronous error handlers // Also supports a callback so you can process errors asynchronously // and the EnumStream won't call `end` until all errors are handled // When everything has finished and the stream has ended
API
constructor( [stream[, options]] ) -> EnumStream
Creates a new EnumStream.
Alias: EnumStream.create( stream[, options] )
Options:
// How many objects to keep in memory concurrency: 1
.setStream(stream) -> this
Sets this.stream
and registers listeners on the stream. Useful for setting up the EnumStream flow before having access to the underlying stream:
const es = EnumStream; // Maybe you're passing `es` around and consumers add// their own handlers:// ... // Some middleware{ return es} // and at the end of the configuration, you get the stream source{ return dbusers ;}
.map( functor(obj) ) -> this
Maps the data object to a new value:
EnumStream
.mapAsync( functor(obj, callback) )
Same as .map
, but with a callback:
EnumStream
.filter( functor(obj) )
Filters values from the stream. If functor
returns true, the value stays. If functor
returns false, the value is omitted.
EnumStream
.filterAsync( functor(obj, callback) )
Same as .filter
, but with a callback:
EnumStream
.forEach( functor(obj) )
Registers an iteration handler:
EnumStream
.reduce( functor(newValue, obj) )
Consumes and reduces the stream to a single value. NOTE: you cannot add new enumeration actions after reducing:
EnumStream // Create a report
.end( function([reducedValue]) )
Registers an end handler. Called when the stream has ended:
EnumStream
.errors( function(error) )
Registers an error handler. Called when the underlying stream emits an error or when an async action calls back with an error:
EnumStream
.errorsAsync()
Same as .errors
but with a callback to let EnumStream know you're done processing.
EnumStream