ObjectStreams
Node.js utility for working with streams of objects.
Install
$ npm install object-streams
Usage
var streams = ; someStream;
API
ObjectStreams provides a set of Node.js Transform streams.
Each of these ObjectStreams
work on a stream of objects to alter them, filter
them, or to interact with them in some way.
There are two ways to use ObjectStreams. The first is to use the built-in pipe
method of streams to pipe the results from a previous stream to an ObjectStream.
The second is to take advantage of an ObjectStream's methods which automatically
call pipe
and return the new stream. For example, the following three pieces
of code do the exact same thing.
someStream ;
someStream ;
someStream ;
In the last example, streams()
is a no-op ObjectStream stream that passes all
objects straight through allowing the chain to start from a non-ObjectStream.
There are two types of ObjectStreams: iterators, and reducers. Iterators are used to affect each object in some way as it comes through the stream. Reducers are used to calculate totals from objects that are processed in the stream.
Iterators
Each iterator uses an iterator function to process each object. All iterators allow for synchronous and asynchronous execution:
// Synchronous { return objectisFile;} // Asynchronous { fs;}
In the following documentation we use synchronous examples, but each one can be
done asynchronously by including a done
argument as shown above.
Map
Maps the object to another value.
stream;
MultiMap
Maps the object to zero or more other values (expects an array to be returned).
stream;
Filter
Filters out any objects that don't meet a certain condition.
stream;
Each
Allows something to be done while sending the original value on through.
stream;
Reducers
Each reducer can work in two modes. It can either convert the stream to the single reduced value, or it can pass objects through the stream and return the reduced value in a callback. This allows you to, for example, pass a count to the final destination, or to get a count without changing anything. Example:
// passes a number as the final count at the end of the streamstream;stream; // passes each object through and calls the callback at the end of the streamstream;
Count
Counts all objects in the stream.
stream;
Sum
Adds up all objects in the stream. Use map
to provide a number to add if your
stream is not a stream of numbers.
stream;
Stats
Provides stats for count, sum, min, max, and the sum of all the square roots which can be useful in determining the standard deviation.
stream;
Split
You can split an object stream into many using split
. You will get a new
stream for as many arguments as your splitter function provides. Whatever is
returned in your splitter function will be returned from the split
function.
stream;
Extending ObjectStream
You may add new iterators, reducers, or other types of functionality useful in
chaining object streams by using the add
method.
var streams = ;streams;
The IterateStream
, ReduceStream
, and base ObjectStream
transform classes
are available for use if needed.
var streams = ;streamsObjectStream;streamsIterateStream;streamsReduceStream;
Converting JSON text to streams of objects
If you need to convert a stream of JSON text into objects, check out https://github.com/dominictarr/JSONStream which will parse JSON as it streams in.
LevelUP
Use withvar levelup = ;var streams = ; var db = streams; db; // only grab the valuesdb; db ;