time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

factory.js (2642B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var copy = require( '@stdlib/utils/copy' );
     24 var ArrayStream = require( './main.js' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns a function for creating readable streams from array-like objects.
     31 *
     32 * @param {Options} [options] - stream options
     33 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode
     34 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings`
     35 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before pausing streaming
     36 * @param {string} [options.sep='\n'] - separator used to join streamed data
     37 * @param {Function} [options.serialize] - custom serialization function
     38 * @param {integer} [options.dir=1] - iteration direction
     39 * @returns {Function} stream factory
     40 *
     41 * @example
     42 * var Float64Array = require( '@stdlib/array/float64' );
     43 * var randu = require( '@stdlib/random/base/randu' );
     44 *
     45 * var arr = new Float64Array( 10 );
     46 * var i;
     47 * for ( i = 0; i < arr.length; i++ ) {
     48 *     arr[ i ] = randu();
     49 * }
     50 *
     51 * var opts = {
     52 *     'sep': ',',
     53 *     'objectMode': false,
     54 *     'encoding': 'utf8',
     55 *     'highWaterMark': 64
     56 * };
     57 *
     58 * var createStream = factory( opts );
     59 *
     60 * // Create 10 identically configured streams...
     61 * var streams = [];
     62 * for ( i = 0; i < 10; i++ ) {
     63 *     streams.push( createStream( arr ) );
     64 * }
     65 */
     66 function factory( options ) {
     67 	var opts;
     68 	if ( arguments.length ) {
     69 		opts = copy( options, 1 );
     70 	} else {
     71 		opts = {};
     72 	}
     73 	return createStream;
     74 
     75 	/**
     76 	* Returns a readable stream from an array-like object.
     77 	*
     78 	* @private
     79 	* @param {Collection} src - source array-like object
     80 	* @throws {TypeError} must provide an array-like object
     81 	* @throws {TypeError} options argument must be an object
     82 	* @throws {TypeError} must provide valid options
     83 	* @returns {ArrayStream} Stream instance
     84 	*/
     85 	function createStream( src ) {
     86 		return new ArrayStream( src, opts );
     87 	}
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = factory;