filterstream
filterstream
is a function based filter for streams inspired per gulp-filter
but no limited to Gulp nor to objectMode streams.
Installation
First install filterstream
in you project:
npm install --save filterstream
Getting started
There are 3 scenarios of use :
Simple filter
var FilterStream = require('filterstream');
var filter = new FilterStream(function(chunk, encoding, cb) {
if(itmustbefiltered) {
cb(true);
} else {
cb(false);
}
});
process.stdin
.pipe(filter)
.pipe(process.stdout);
Filter and restore
var FilterStream = require('filterstream');
var filter = new FilterStream(function(chunk, encoding, cb) {
if(itmustbefiltered) {
cb(true);
} else {
cb(false);
}
}, {
restore: true
});
filter.pipe(process.stdout);
filter.restore.pipe(process.stderr);
Filter and restore as a passthrough stream
Let's be hype!
var FilterStream = require('filterstream');
var filter = new FilterStream(function(chunk, encoding, cb) {
if(itmustbefiltered) {
cb(true);
} else {
cb(false);
}
}, {
restore: true,
passthrough: true
});
process.stdin.pipe(filter)
.pipe(mySuperTransformStream)
.pipe(filter.restore)
.pipe(process.stdout)
Note that in this case, this is your responsibility to end the restore stream
by piping in another stream or ending him manually.
## API
stream:Stream FilterStream(filterCallback:Function, options:Object)
Filter piped in streams according to the given filterCallback
. Options are
passed in as is in the various stream instances spawned by this module. So,
to use the objectMode, simply pass in the options.objectMode
value set to
true
.
options.restore
Set to true
, this option create a readable stream allowing you to use the
filtered chunks elsewhere. The restore stream is exposed in the FilterStream
instance as a restore
named property.
options.passthrough
Set to true
, this option change the restore stream nature from a readable
stream to a passthrough one, allowing you to reuse the filtered chunks in an
existing pipeline.
Contribute
Feel free to submit us your improvements. To do so, you must accept to publish
your code under the MIT license.
To start contributing, first run the following to setup the development
environment:
git clone git@github.com:nfroidure/filterstream.git
cd filterstream
npm install
Then, run the tests:
npm test
Stats