Skip to content

create a transform stream that runs a function over all collected elements

Notifications You must be signed in to change notification settings

hackergrrl/collect-transform-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

collect-transform-stream

create a transform stream that runs a function over all collected elements

Sometimes you end up in a situation where you have a stream pipeline like

pump(source, transform, destination)

but you have another transform function that needs to process all elements in the stream before passing them on, e.g. filtering duplicates from an unordered stream:

pump(source, collectAndFilter, transform, destination)

collect-transform-stream is a tiny module that provides this.

Usage

var uniq = require('uniq')
var from = require('from2')
var collect = require('concat-stream')
var collectTransform = require('collect-transform-stream')

var source = from.obj(new Array(500).fill(0).map(function () {
  return Math.floor(Math.random() * 10)
}))

var dedupe = collectTransform(function (nums) {
  return uniq(nums)
})

var dest = collect({encoding:'object'}, console.log)

source.pipe(dedupe).pipe(dest)

outputs

0
1
2
3
4
5
6
7
8
9

API

var collect = require('collect-transform-stream')

collect(fn[, cb])

Create a Transform stream that runs the function fn on all incoming objects at once, then pipes elements individually onward to the next stream in the pipeline.

Use return result to use synchronously; call cb(err, result) to use asynchronously.

Install

With npm installed, run

$ npm install collect-transform-stream

License

ISC

About

create a transform stream that runs a function over all collected elements

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published