match-sink
v1.0.3
Published
pipe data to a stream and react to single or multiple matches
Downloads
8
Readme
match-sink
Pipe data to a stream and react to single or multiple matches
Features
- Pipe data to a stream and get notified when something matches
- Match once or multiple times
Can be useful for various things. Imagine some data stream you want to react to. It could be a server listening on some port so you know when to start interacting with it.
Install
$ npm install match-sink -S
Usage
The following example prints out the total file size in the current directory:
const sink = require('match-sink')
const spawn = require('child_process').spawn
spawn('ls', [ '-lh' ]).stdout.pipe(sink(/total\s+(\S+)/gi, match => {
console.log(match[1])
}))
Api
const sink = require('match-sink')
Returns a stream constructor.
const stream = sink(opts, cb)
Returns a writable stream.
opts
(object|regexp) Either options object withregexp
andmatchAll
properties or a single regular expression.cb
(function) Callback. Will act as an iterator if there are several matches andmatchAll
property is set totrue
. Ifopts
is a regular expression the callback will be called at the first match only, if ever.
License
All code, unless stated otherwise, is licensed under the WTFPL
.