Skip to content

flow-io/from-array-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

from-array

NPM version Build Status Coverage Status Dependencies

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,

var fromArray = 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,

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

To set the readable stream options,

var opts = {
		'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.

var opts = {
		'objectMode': true,
		'encoding': 'utf8',
		'highWaterMark': 8
	};

var factory = fromArray.factory( opts );

var streams = new Array( 10 ),
	data;

// Create many streams configured identically but reading different datasets...
for ( var i = 0; i < streams.length; i++ ) {
	data = new Array( 100 );
	for ( var j = 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.

var fromArray = require( 'flow-from-array' ).objectMode;

fromArray( ['b','e','e','p'] )
	.pipe( process.stdout );

Examples

var append = require( 'flow-append' ).objectMode,
	fromArray = require( 'flow-from-array' );

// Create some data...
var data = new Array( 1000 );
for ( var i = 0; i < data.length; i++ ) {
	data[ i ] = Math.random();
}

// Create a readable stream:
var readableStream = 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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published