factory.js (2546B)
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 join stream factory. 32 * 33 * @param {Options} [options] - stream options 34 * @param {string} [options.sep='\n'] - separator used to join streamed data 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 `Buffer` level for when `write()` starts returning `false` 38 * @param {boolean} [options.allowHalfOpen=false] - specifies whether the stream should remain open even if one side ends 39 * @param {boolean} [options.readableObjectMode=false] - specifies whether the readable side should be in object mode 40 * @throws {TypeError} options argument must be an object 41 * @returns {Function} join stream factory 42 * 43 * @example 44 * var opts = { 45 * 'sep': '\t', 46 * 'objectMode': true, 47 * 'encoding': 'utf8', 48 * 'highWaterMark': 64 49 * }; 50 * 51 * var factory = streamFactory( opts ); 52 * 53 * // Create 10 identically configured streams... 54 * var streams = []; 55 * var i; 56 * for ( i = 0; i < 10; i++ ) { 57 * streams.push( factory() ); 58 * } 59 */ 60 function streamFactory( options ) { 61 var opts; 62 if ( arguments.length ) { 63 if ( !isObject( options ) ) { 64 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 65 } 66 opts = copy( options ); 67 } else { 68 opts = {}; 69 } 70 return joinStream; 71 72 /** 73 * Creates a transform stream for joining streamed data. 74 * 75 * @private 76 * @throws {TypeError} must provide valid options 77 * @returns {JoinStream} join stream 78 */ 79 function joinStream() { 80 return new Stream( opts ); 81 } 82 } 83 84 85 // EXPORTS // 86 87 module.exports = streamFactory;