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: 3 months ago
JSON representation
create a transform stream that runs a function over all collected elements
- Host: GitHub
- URL: https://github.com/hackergrrl/collect-transform-stream
- Owner: hackergrrl
- Created: 2017-03-24T16:53:48.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-24T17:19:58.000Z (over 8 years ago)
- Last Synced: 2025-03-09T05:39:14.147Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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