Skip to content

Repository files navigation

from-array

NPM versionBuild StatusCoverage StatusDependencies

Converts an array to a readable stream.

Installation

$ npm install flow-from-array

For use in the browser, use browserify.

Usage

To use the module,

varfromArray=require('flow-from-array');

fromArray( arr[, options] )

Returns a readable stream where each emitted datum is an element from the input array.

To convert an array to a readable stream,

varstream=fromArray([1,2,3,4]);

To set the readable stream options,

varopts={'objectMode': true,'encoding': 'utf8','highWaterMark': 8};stream=fromArray(['b','e','e','p'],opts);

fromArray.factory( [options] )

Returns a reusable stream factory. The factory method ensures streams are configured identically by using the same set of provided options.

varopts={'objectMode': true,'encoding': 'utf8','highWaterMark': 8};varfactory=fromArray.factory(opts);varstreams=newArray(10),data;// Create many streams configured identically but reading different datasets...for(vari=0;i<streams.length;i++){data=newArray(100);for(varj=0;j<data.length;j++){data[j]=Math.random();}streams[i]=factory(data);}

fromArray.objectMode( arr[, options] )

This method is a convenience function to create readable streams which always operate in objectMode. The method will always override the objectMode option in options.

varfromArray=require('flow-from-array').objectMode;fromArray(['b','e','e','p']).pipe(process.stdout);

Examples

varappend=require('flow-append').objectMode,fromArray=require('flow-from-array');// Create some data...vardata=newArray(1000);for(vari=0;i<data.length;i++){data[i]=Math.random();}// Create a readable stream:varreadableStream=fromArray(data);// Pipe the data:readableStream.pipe(append('\n')).pipe(process.stdout);

To run the example code from the top-level application directory,

$ node ./examples/index.js

Notes

This stream is a Streams2 version of event-stream and its readArray() method.

When in objectMode, an array cannot contain null or undefined values. An array containing either of these values will emit an error and close.

When not in objectMode, all array values are buffered. This means that anything which is not a buffer or a string is coerced into being a string. Values are stringified according to the following conventions:

  • undefined: "undefined"
  • null: "null"
  • number: <number>.toString()
  • boolean: <boolean>.toString()
  • function: <function>.toString()
  • array: JSON.stringify( <array> )
  • object: JSON.stringify( <object> )

With the exception of arrays and objects, the conventions follow the ES5 specification. arrays and objects are more conveniently stringified.

Note that the stringified values are buffered according to the encoding option. If the encoding is null (default), buffering assumes utf8 encoding.

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

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-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ open reports/coverage/lcov-report/index.html

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.

About

Converts an array to a readable stream.

Resources

Stars

2 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages