conditional-stream is a duplex stream that only passes data when a set function returns true
- node v0.10+ (this uses streams2)
- npm install conditional-stream
to filter/limit a stream to only numbers
onlyNumbers.js
var CondStream = require('./index')
, cond
cond = new CondStream(function(input) {
// return true if input string is of type Number
return (!isNaN(Number(input.toString())))
})
process.stdin.pipe(cond).pipe(process.stdout)
console
> echo 2 | node onlyNumbers.js
2
> echo a | node onlyNumbers.js
>
MIT