npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

through2-reduce

v1.1.1

Published

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

Downloads

3,059

Readme

through2-reduce

NPM

through2-reduce is a thin wrapper around 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.


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