Skip to content

th507/stream-filter2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

filter2

Build Status NPM version License NPM desciption

A versatile stream filter for Node.js.

This package is not affliated with Tim-Smart’s filter.

Install

$ npm install filter2

Usage

var filter = require('filter2')

Passthrough (do nothing)

fs.createReadStream('./foo.txt')
  .pipe(filter())

or call with filter.filter for better symmetry.

Get the first 5 chunks

fs.createReadStream('./foo.txt')
  .pipe(filter.head(5))

Get the last 5 chunks

fs.createReadStream('./foo.txt')
  .pipe(filter.tail(5))

Get the filtered chunks

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().

Get a custom set in chunks

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

Break up a stream and reassemble it

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

Wrap around stream.Transform

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
  })

License

Copyright (c) 2015 Jingwei "John" Liu

Licensed under the MIT license.

About

A versatile stream filter for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published