time-to-botec

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

factory.js (3591B)


      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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
     24 var copy = require( '@stdlib/utils/copy' );
     25 var ConstantStream = require( './main.js' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns a function for creating readable streams which always stream the same value.
     32 *
     33 * @param {(string|Buffer|Uint8Array|*)} [value] - value to stream
     34 * @param {Options} [options] - stream options
     35 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode
     36 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings`
     37 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before pausing streaming
     38 * @param {string} [options.sep='\n'] - separator used to join streamed data
     39 * @param {NonNegativeInteger} [options.iter] - number of iterations
     40 * @returns {Function} stream factory
     41 *
     42 * @example
     43 * var opts = {
     44 *     'sep': ',',
     45 *     'objectMode': false,
     46 *     'encoding': 'utf8',
     47 *     'highWaterMark': 64
     48 * };
     49 *
     50 * var createStream = factory( opts );
     51 *
     52 * // Create 10 identically configured streams...
     53 * var streams = [];
     54 * var i;
     55 * for ( i = 0; i < 10; i++ ) {
     56 *     streams.push( createStream( i.toString() ) );
     57 * }
     58 */
     59 function factory( value, options ) {
     60 	var nargs;
     61 	var opts;
     62 	var fcn;
     63 	var FLG;
     64 
     65 	nargs = arguments.length;
     66 	if ( nargs === 0 ) {
     67 		opts = {};
     68 		FLG = true;
     69 	} else if ( nargs === 1 ) {
     70 		// Check (imperfectly!) whether we were provided an "options" object...
     71 		if (
     72 			value !== null &&
     73 			typeof value === 'object' &&
     74 			(
     75 				hasOwnProp( value, 'sep' ) ||
     76 				hasOwnProp( value, 'iter' ) ||
     77 				hasOwnProp( value, 'objectMode' ) ||
     78 				hasOwnProp( value, 'encoding' ) ||
     79 				hasOwnProp( value, 'highWaterMark' )
     80 			)
     81 		) {
     82 			opts = copy( value, 1 );
     83 			FLG = true;
     84 		} else {
     85 			opts = {};
     86 		}
     87 	} else { // nargs > 1
     88 		opts = copy( options, 1 );
     89 	}
     90 	if ( FLG ) {
     91 		fcn = createStream1;
     92 	} else {
     93 		fcn = createStream2;
     94 	}
     95 	return fcn;
     96 
     97 	/**
     98 	* Returns a readable stream which always streams the same value.
     99 	*
    100 	* @private
    101 	* @param {(string|Buffer|Uint8Array|*)} value - value to stream
    102 	* @throws {TypeError} in binary mode, value to stream must be a string, Buffer, or Uint8Array
    103 	* @throws {TypeError} options argument must be an object
    104 	* @throws {TypeError} must provide valid options
    105 	* @returns {ConstantStream} Stream instance
    106 	*/
    107 	function createStream1( value ) {
    108 		return new ConstantStream( value, opts );
    109 	}
    110 
    111 	/**
    112 	* Returns a readable stream which always streams the same value.
    113 	*
    114 	* @private
    115 	* @throws {TypeError} in binary mode, value to stream must be a string, Buffer, or Uint8Array
    116 	* @throws {TypeError} options argument must be an object
    117 	* @throws {TypeError} must provide valid options
    118 	* @returns {ConstantStream} Stream instance
    119 	*/
    120 	function createStream2() {
    121 		return new ConstantStream( value, opts );
    122 	}
    123 }
    124 
    125 
    126 // EXPORTS //
    127 
    128 module.exports = factory;