cop

Filter object streams

cop - filter object stream

The cop Node.js module is a Transform stream (in object mode) which is configurable to emit specific properties of objects written to it. Furthermore, if provided with a filter function, cop can be used to massage data.

var cop = require('cop')
  , Readable = require('stream').Readable
 
var objs = [
  { thing: 'My hovercraft ' }
, { thing: 'is full ' }
, { thing: 'of eels.\n' }
]
 
var reader = new Readable({ objectMode:true })
  , length = objs.length
  , i = 0
 
reader._read = function () {
  reader.push(< length ? objs[i++] : null)
}
 
reader
  .pipe(cop('thing'))
  .pipe(process.stdout)
var cop = require('cop')
  , fstream = require('fstream')
  , reader = fstream.Reader({ path:process.cwd() })
 
reader
  .pipe(cop(filter))
  .pipe(process.stdout)
 
function filter (obj) {
  return obj ? obj['path'] + '\n' : undefined
}
  • key Name of the property of which to emit the value

Provided a key, cop will emit the matching property of an object written to it. If the object has no matching property nothing is emitted.

  • filter Filter function which is applied to each object written to the stream

Alternatively a filter function can be provided, which is executed before data is emitted, hence, provides an opportunity to massage the data.

MIT License