Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A "reduce" method for your Node streams.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 README.md
Octocat-spinner-32 index.js
Octocat-spinner-32 package.json
Octocat-spinner-32 reductionist.js
README.md

reductionist Build Status

A through stream that acts as a streaming equivalent for Javascript's reduce method, built off @dominictarr's map-stream.

Installation

npm install reductionist

Usage

reductionist(iterator, memo, [options])

The iterator callback takes three arguments. memo is the value of the existing reduction, num is the data being input, and next is a callback that determines the next value of memo. It's more or less equivalent to underscore.js.

This stream'll emit running total of the numbers written to it:

var reduce = require('reductionist')
  , es = require('event-stream');

var stream = reduce(function(memo, num, next) {
    return next(null, memo + num);
}, 0);

stream.pipe(es.stringify())
      .pipe(process.stdout);

stream.write(1); // 1
stream.write(2); // 3
stream.write(3); // 6
stream.write(4); // 10
stream.write(5); // 15

Passing an error to the callback's first parameter will cause the stream to emit it and stop accepting input.

By default, reductionist will output the new value of memo each time it changes. But by passing { every: false } as an option, the stream will only emit a single data event, once it's been closed: the final value for memo.

var reduce = require('reductionist')
  , request = require('request')

var stream = reduce(function(memo, chunk, next) {
  return next(null, memo + chunk.length, next);
}, 0, {
  every: false
})

request.get('http://instagram.com/')
  .pipe(stream)
  .once('data', function(size) {
    console.log(size);
  })
Something went wrong with that request. Please try again.