Creates a transform stream which maps each streamed value to another value.
$ npm install flow-mapFor use in the browser, use browserify.
varstream=require('flow-map');Creates a transform stream which maps each streamed value to another value using a provided function.
varmStream=stream(map);// Note: the index is zero-based...functionmap(value,idx){value=parseFloat(value);return(value*idx).toString();}// Pipe the output from the map stream to stdout:mStream.pipe(process.stdout);// Write data to the stream:mStream.write('5');// => 0mStream.write('4');// => 4mStream.write('10');// => 20// End the stream:mStream.end();The function accepts the following options:
- highWaterMark: specifies the
Bufferlevel at whichwrite()calls start returningfalse. Default:16(i.e., 16 queued items). - allowHalfOpen: specifies whether a stream should remain open even if one side ends. Default:
false.
To set streamoptions,
varopts={'highWaterMark': 64,'allowHalfOpen': true};varmStream=stream(map,opts);Note: the returned streamalways operates in objectMode.
Creates a reusable stream factory. The factory method ensures streams are configured identically by using the same set of provided options.
varopts={'highWaterMark': 64};varfactory=stream.factory(opts);// Create 10 identically configured streams...varstreams=[];for(vari=0;i<10;i++){streams.push(factory(map));}This method is a convenience function to create streams which always operate in objectMode. The method will always override the objectMode option in options.
vardata={'value': 5};functionmap(data,idx){returndata.value*10;}varmStream=stream.objectMode(map);mStream.pipe(process.stdout);mStream.write(data);// => 50mStream.end();Note: this method behaves the same as the main method and is provided to maintain API consistency with other flow modules.
varcreateStream=require('flow-map');functionmap(value,idx){returnvalue*idx;}functiontoString(value){returnvalue.toString()+'\n';}varmStream=createStream(map),tsStream=createStream(toString);mStream.pipe(tsStream).pipe(process.stdout);for(vari=0;i<1000;i++){mStream.write(Math.random());}mStream.end();To run the example code from the top-level application directory,
$ DEBUG=* node ./examples/index.jsTo use the module as a general utility, install the module globally
$ npm install -g flow-mapUsage: flow-map [options] module
Options:
-h, --help Print this message.
-V, --version Print the package version.
--split sep Separator used to split incoming data. Default: '/\\r?\\n/'.
--join sep Separator used to join outgoing data. Default: '\n'.
-hwm, --highwatermark hwm Specify how much data can be buffered into memory
before applying back pressure. Default: 16.
-aho, --allowhalfopen Keep the stream open if either the readable or writable
side ends. Default: false.The flow-map command is available as a standard stream.
$ <stdout>| flow-map <module>|<stdin>If the split separator is a regular expression, ensure that the
splitoption is properly escaped.# Not escaped... $ <stdout>| flow-map <module> --split '/\r?\n/'# Escaped... $ <stdout>| flow-map <module> --split '/\\r?\\n/'
$ echo -n $'1\n2\n3\n4\n'| flow-map ./examples/script.js
# => 0# => 2# => 6# => 12For local installations, modify the above command to point to the local installation directory; e.g.,
$ echo -n $'1\n2\n3\n4\n'| ./node_modules/.bin/flow-map ./examples/script.jsOr, if you have cloned this repository and run npm install, modify the command to point to the executable; e.g.,
$ echo -n $'1\n2\n3\n4\n'| node ./bin/cli ./examples/script.jsUnit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make testAll new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-covIstanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-covCopyright © 2014-2015. The Flow.io Authors.