A versatile stream filter for Node.js.
This package is not affliated with Tim-Smart’s filter.
$ npm install filter2
var filter = require('filter2')
fs.createReadStream('./foo.txt')
.pipe(filter())
or call with filter.filter
for better symmetry.
fs.createReadStream('./foo.txt')
.pipe(filter.head(5))
fs.createReadStream('./foo.txt')
.pipe(filter.tail(5))
fs.createReadStream('./foo.txt')
.pipe(filter(function(chunk, encoding, next) {
// returns true to make chunks flow downstream
return /bar/.test(chunk, encoding, next)
}))
Filter function get the same arguments as through2.
Usually filter
only makes sense when using after filter.split()
.
function MySet(n) {
this._array = Array(n)
}
MySet.prototype.push = function(thing) {
// filter input as you wish
this._array.push(thing)
}
MySet.prototype.forEach = function() {
Array.prototype.forEach.apply(this._array, arguments)
}
var mySet = new MySet(4)
fs.createReadStream('./foo.txt')
.pipe(filter.some(mySet))
// each chunk is pushed into mySet
// and when upstream drains, it
// returns a set of chunks that mySet.forEach returns
This is just an alias of Matteo Collina's split2. Please refers to split2 README for more details.
fs.createReadStream('./foo.txt')
.pipe(filter.split())
// split and rearrange chunks by line break
This is just an alias of Rod Vagg's through2. Please refers to through2 README for more details.
fs.createReadStream('./foo.txt')
.pipe(filter.through(function(chunk, encoding, next) {
// write your transform function here
})
Copyright (c) 2015 Jingwei "John" Liu
Licensed under the MIT license.