time-to-botec

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

factory.js (2623B)


      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 isObject = require( '@stdlib/assert/is-plain-object' );
     24 var copy = require( '@stdlib/utils/copy' );
     25 var Stream = require( './main.js' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Creates a reusable debug stream factory.
     32 *
     33 * @param {Options} [options] - stream options
     34 * @param {boolean} [options.objectMode=false] - specifies whether the stream should operate in object mode
     35 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false`
     36 * @param {boolean} [options.allowHalfOpen=false] - specifies whether the stream should remain open even if one side ends
     37 * @param {boolean} [options.readableObjectMode=false] - specifies whether the readable side should be in object mode
     38 * @throws {TypeError} options argument must be an object
     39 * @returns {Function} debug stream factory
     40 *
     41 * @example
     42 * var opts = {
     43 *     'objectMode': true,
     44 *     'highWaterMark': 64
     45 * };
     46 *
     47 * var factory = streamFactory( opts );
     48 *
     49 * // Assign each stream to a separate debug namespace...
     50 * var streams = [];
     51 * var i;
     52 * for ( i = 0; i < 10; i++ ) {
     53 *     streams.push( factory( 'stream '+i ) );
     54 * }
     55 */
     56 function streamFactory( options ) {
     57 	var opts;
     58 	if ( arguments.length ) {
     59 		if ( !isObject( options ) ) {
     60 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     61 		}
     62 		opts = copy( options );
     63 	} else {
     64 		opts = {};
     65 	}
     66 	return debugStream;
     67 
     68 	/**
     69 	* Creates a transform stream for debugging stream pipelines.
     70 	*
     71 	* @private
     72 	* @param {string} name - debug namespace
     73 	* @param {Callback} [clbk] - callback to invoke upon receiving data
     74 	* @throws {TypeError} must provide valid options
     75 	* @throws {TypeError} must provide a valid callback argument
     76 	* @returns {DebugStream} debug stream
     77 	*/
     78 	function debugStream( name, clbk ) {
     79 		opts.name = name;
     80 		if ( arguments.length > 1 ) {
     81 			return new Stream( opts, clbk );
     82 		}
     83 		return new Stream( opts );
     84 	}
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = streamFactory;