Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created September 17, 2012 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/3737592 to your computer and use it in GitHub Desktop.
Save Raynos/3737592 to your computer and use it in GitHub Desktop.

Chain streams

Chain streaming operations together

Example

// Create a chaining stream based on a stream of todo data chunks
chain(todos)
    // Map them through a Todo constructor
    .map(Todo)
    // pluck the elements.root property
    .pluck("elements.root")
    .forEach(function (elem) {
        todoList.appendChild(elem)
    })
var map = require("lazy-map-stream")
, forEach = require("forEach-stream")
, pluck = require("pluck-stream")
module.exports = chain
function chain(stream) {
var curr = stream
, self = {
map: apply(map)
, forEach: apply(forEach)
, pluck: apply(pluck)
}
return self
function apply(f) {
return function () {
var args = [].slice.call(arguments)
, args.unshift(curr)
curr = f.apply(null, args)
return self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment