Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hackergrrl/collect-transform-stream

create a transform stream that runs a function over all collected elements
https://github.com/hackergrrl/collect-transform-stream

Last synced: 1 day ago
JSON representation

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

Awesome Lists containing this project

README

        

# 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

```js
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:

```js
pump(source, collectAndFilter, transform, destination)
```

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

## Usage

```js
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

```js
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](https://npmjs.org/) installed, run

```
$ npm install collect-transform-stream
```

## License

ISC