# through2-reduce

> A through2 wrapper to emulate Array.prototype.reduce for streams.

Latest version **1.1.1** (published 2016-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install through2-reduce
pnpm add through2-reduce
yarn add through2-reduce
bun add through2-reduce
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2016-02-05 |
| First published | 2013-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Bryce B. Baril |
| Maintainers | bryce |
| Keywords | streams, through, through2, reduce |

## Links

- npm: https://www.npmjs.com/package/through2-reduce
- Repository: https://github.com/brycebaril/through2-reduce
- Issues: https://github.com/brycebaril/through2-reduce/issues
- npm.io page: https://npm.io/package/through2-reduce

## Dependencies (2)

- [xtend](https://npm.io/package/xtend.md) ~4.0.1
- [through2](https://npm.io/package/through2.md) ~2.0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2016-02-05
- 1.1.0 — 2016-01-19
- 1.0.1 — 2015-11-21
- 1.0.0 — 2015-06-17
- 0.3.0 — 2015-02-10
- 0.2.4 — 2015-02-05
- 0.2.3 — 2014-01-31
- 0.2.2 — 2014-01-31
- 0.2.1 — 2013-10-29
- 0.2.0 — 2013-09-20
- 0.1.0 — 2013-08-07

## README

through2-reduce
===============

[![NPM](https://nodei.co/npm/through2-reduce.png)](https://nodei.co/npm/through2-reduce/)

`through2-reduce` is a thin wrapper around [through2](http://npm.im/through2) that works like `Array.prototype.reduce` but for streams.

This is a *much* less common use-case with streams, but it can occasionally be useful to do a Reduce function on a stream.

**EXPERIMENTAL** This is a bit of a bizarre one, so I wouldn't be surprised if there are some dangerous edge cases around flushing and pausing and such. Use at your own risk.

This stream will only ever emit a *single* chunk. For more traditional `stream.Transform` filters or transforms, consider `through2` `through2-filter` or `through2-map`.

Also, if your stream never ends, Reduce will never end.

```js

var reduce = require("through2-reduce")

var sum = reduce({objectMode: true}, function (previous, current) { return previous + current })

// vs. with through2:
function combine (value, encoding, callback) {
  if (this.total == undefined) {
    this.total = value
    return callback()
  }
  this.total += value
  return callback()
}
function flush (callback) {
  this.push(this.total)
  return callback()
}
var sum = through2({objectMode: true}, combine, flush)

// Then use your reduce: (e.g. source is an objectMode stream of numbers)
source.pipe(sum).pipe(sink)

// Works like `Array.prototype.reduce` meaning you can specify a function that
// takes up to three* arguments: fn(previous, current, index) AND you can specify
// an initial value
var mean = reduce({objectMode: true}, function (prev, curr, index) {
  return prev - (prev - curr) / (index + 1)
}, 0)

```

*Differences from `Array.prototype.reduce`:
  * No fourth `array` callback argument. That would require realizing the entire stream, which is generally counter-productive to stream operations.
  * `Array.prototype.reduce` doesn't modify the source Array, which is somewhat nonsensical when applied to streams.

API
----

`reduce([options,] fn [,initial])`

Create a Reduce *instance*

`reduce.ctor([options,] fn [,initial])`

Create a Reduce *class*

`reduce.obj([options,] fn [,initial])`

Create a Reduce *instance* that defaults to `objectMode: true`.

`reduce.objCtor([options,] fn [,initial])`

Just like ctor, but with `objectMode: true` defaulting to true.

Options
-------

  * wantStrings: Automatically call chunk.toString() for the super lazy.
  * all other through2 options

LICENSE
=======

MIT

---
_Source: https://npm.io/package/through2-reduce · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
