Useful stream sources, transforms and sinks for Simple Streams.
##Documentation ###Sources
###Transforms
###Sinks
##Sources
### fromArray(array) -> stream Creates an stream from an array.var arrayStream = stream.fromArray(numbers)
var readStream = fs.createReadStream('input.txt', {encoding: 'utf8'})
var streamStream = stream.fromReadableStream(readStream)
##Transforms
### map(stream, mapFn) -> stream Create an stream that applies a map function to transform each value of the source stream.var mapStream = stream.map(someNumberStream, function(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(each, cb) {
cb(null, each * 2)
})
var evenNumbersStream = stream.filter(someNumberStream, function(each) {
return (each % 2) == 0
})
var evenNumbersStream = stream.filter(someNumberStream, function(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
### toArray(stream) -> continuable Reads the source stream and writes the results to an array.stream.toArray(someStream)(function(err, array) {
console.log(array)
})
var writeStream = fs.createWriteStream('output.txt')
stream.toWritableStream(stream, writeStream, 'utf8')(function(err) {
console.log('done')
})
stream.forEach(someStream, function(data) {
console.log(data)
})(function(err) {
console.log('end')
})
stream.forEachAsync(someStream, function(data, cb) {
console.log(data)
setTimeout(cb, 100)
}(function(err) {
console.log('end')
})
##Contributors This project was created by Mirko Kiefer (@mirkokiefer).