chain-stream

Chain stream operations together

chain-stream

Chain stream operations together

The result of chain is a stream at every step. In addition to being a stream it has the methods demonstrated below

var chain = require("..")
    , from = require("read-stream").fromArray
 
// Creates a stream from an array
// You can also pass any readable stream in
chain([1,2,3,4,5])
    // [1, 2, 3, 4, 5]
    .log("initial state")
    // Map every chunk of data flowing through the stream to something else
    .map(function (x) { return x * 5 })
    // [5, 10, 15, 20, 25]
    .log("mapped state")
    // Filter the stream by dropping chunks you don't care about
    .filter(function (x) { return x % 2 })
    // [5, 15, 25]
    .log("filtered state")
    // Accumulate state about the stream
    .reductions(function (acc, x) { return acc + x }, 0)
    // [5, 20, 45]
    .log("reducted state")
    // Map the each chunk to a stream. This becomes a stream of streams
    .map(function (x) { return from([x, x * 2, x * 3]) })
    // Flatten the stream of streams into a single stream
    .flatten()
    .value(function (state) {
        // [5, 10, 15, 20, 40, 60, 45, 90, 135]
        console.log("final state", state)
    })

log and value are just utility methods that make the debugging easier.

npm install chain-stream

  • Raynos