factory.js (1729B)
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 EmptyStream = require( './main.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a function for creating "empty" readable streams. 31 * 32 * @param {Options} [options] - stream options 33 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode 34 * @returns {Function} stream factory 35 * 36 * @example 37 * var opts = { 38 * 'objectMode': false 39 * }; 40 * 41 * var createStream = factory( opts ); 42 * 43 * // Create 10 identically configured streams... 44 * var streams = []; 45 * var i; 46 * for ( i = 0; i < 10; i++ ) { 47 * streams.push( createStream() ); 48 * } 49 */ 50 function factory( options ) { 51 var opts; 52 if ( arguments.length > 0 ) { 53 opts = copy( options, 1 ); 54 } else { 55 opts = {}; 56 } 57 return createStream; 58 59 /** 60 * Returns an "empty" readable stream. 61 * 62 * @private 63 * @throws {TypeError} options argument must be an object 64 * @throws {TypeError} must provide valid options 65 * @returns {EmptyStream} Stream instance 66 */ 67 function createStream() { 68 return new EmptyStream( opts ); 69 } 70 } 71 72 73 // EXPORTS // 74 75 module.exports = factory;