simple-stream
v0.3.1
Published
utility functions for simple streams
Downloads
9
Readme
Useful stream sources, transforms and sinks for Simple Streams.
##Documentation ###Sources
###Transforms
###Sinks
##Sources
var arrayStream = stream.fromArray(numbers)
var readStream = fs.createReadStream('input.txt', {encoding: 'utf8'})
var streamStream = stream.fromReadableStream(readStream)
##Transforms
var mapStream = stream.map(someNumberStream, function(err, each) {
return each * 2
})
// pipe the stream to an array:
stream.toArray(mapStream, function(err, res) {
console.log(res)
})
var mapStream = stream.map(someNumberStream, function(err, each, cb) {
cb(null, each * 2)
})
filter(stream, filterFn) -> stream
var evenNumbersStream = stream.filter(someNumberStream, function(err, each) {
return (each % 2) == 0
})
var evenNumbersStream = stream.filter(someNumberStream, function(err, each, cb) {
cb(null, (each % 2) == 0)
})
range
is specified as {from: startIndex, to: endIndex}
where from
and to
are both inclusive.
var rangeStream = stream.range(stream, {from: 10, to: 19})
The current buffer fill ratio can be inspected at any time using bufferFillRatio()
which returns a number between 0..1.
The buffer size can be changed using setBufferSize(bufferSize)
.
var bufferedStream = stream.buffer(someStream, 10)
// inspect buffer size
console.log(bufferedStream.bufferFillRatio())
// change the buffer size later
bufferedStream.setBufferSize(100)
##Sinks
stream.toArray(someStream)(function(err, array) {
console.log(array)
})
var writeStream = fs.createWriteStream('output.txt')
stream.toWritableStream(stream, writeStream, 'utf8')(function() {
console.log('done')
})
stream.forEach(someStream, function(err, data) {
console.log(data)
})(function() {
console.log('end')
})
stream.forEachAsync(someStream, function(err, data, cb) {
console.log(data)
setTimeout(cb, 100)
}(function() {
console.log('end')
})
##Contributors This project was created by Mirko Kiefer (@mirkokiefer).