util streams for Node.js Supports node v0.10
##Streams
###dwarves.toStream( array ) Creates readable data stream from array.
####Arguments
- array(Array): The array to stream
####Example
dwarves.toStream( [ 'a', 'b', 'c' ] )
.on( 'data', function ( d ) { console.log( d ); } );
// -> a
// -> b
// -> c
###dwarves.mapStream( mapper ) Creates transform stream which applies mapper function to data.
####Arguments
- mapper(Function): The function called per streaming data to transform and emit processed data.
#####mapper( data, callback ) arguments
- data(*): A streaming data.
- callback(Function): The function called with emitting data to the next streams.
####Example
var mapper = function ( data, callback ) {
callback( data * data );
};
dwarves.toStream( [ 1, 2, 3 ] )
.pipe( dwarves.mapStream( mapper ) )
.on( 'data', function ( d ) { console.log( d ); } );
// -> 1
// -> 4
// -> 9
###dwarves.reduceStream( initializer, reducer ) Creates transform stream which applies reducer function to aggregate if all data of previous stream have been gotten.
####Arguments
- initializer(*): Initial value of accum.
- reducer(Function): The function called to reduce data.
#####reducer( accum, data, callback ) arguments
- accum(*): An accumlator used to memoize data.
- data(*): A streaming data.
- callback(Function): The function called with emitting data to the next streams.
####Example
var reducer = function ( accum, data, callback ) {
callback( accum + data );
};
dwarves.toStream( [ 1, 2, 3, 4, 5 ] )
.pipe( dwarves.reduceStream( 0, reducer ) )
.on( 'data', function ( d ) { console.log( d ); } );
// -> 15
###dwarves.sampleStream( batchSize ) Creates transform stream which retrieves a random data from stream per batchSize.
####Arguments
- batchSize(Integer): The number of data pool size.
####Example
dwarves.toStream( [ 1, 2, 3, 4, 5 ] )
.pipe( dwarves.sampleStream( 3 ) )
.on( 'data', function ( d ) { console.log( d ); } );
// -> 3
// -> 4
###dwarves.shuffleStream( batchSize ) Creates transform stream which changes order of streaming data per batchSize.
####Arguments
- batchSize(Integer): The number of data pool size.
####Example
dwarves.toStream( [ 1, 2, 3, 4, 5 ] )
.pipe( dwarves.shuffleStream( 3 ) )
.on( 'data', function ( d ) { console.log( d ); } );
// -> 1
// -> 3
// -> 2
// -> 5
// -> 4
###dwarves.groupByStream( keyName, valueName, shuffler ) Creates transform object stream which composed of keys generated from the shuffler callback.
####Arguments
- keyName(String): The key name of grouping key of transformed object stream.
- valueName(String): The key name of grouped values of transformed object stream.
- shuffler(Function): The function called to resolving grouping key name.
#####shuffler( data, callback ) arguments
- data(*): A streaming data
- callback(Function): The function called with key name of this data.
####Example
var shuffler = function ( data, callback ) {
callback( data[ 0 ] );
};
dwarves.toStream( [ 'a', 'ab', 'bb', 'bc' ] )
.pipe( dwarves.groupByStream( 'k', 'v', shuffler ) )
.on( 'data', function ( d ) { console.log( d ); } );
// -> { k: 'a', v: [ 'a', 'ab' ] }
// -> { k: 'b', v: [ 'bb', 'bc' ] }