factory.js (2746B)
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 CircularArrayStream = require( './main.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a function for creating readable streams from circular 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.iter=1e308] - number of iterations 39 * @param {integer} [options.dir=1] - iteration direction 40 * @returns {Function} stream factory 41 * 42 * @example 43 * var Float64Array = require( '@stdlib/array/float64' ); 44 * var randu = require( '@stdlib/random/base/randu' ); 45 * 46 * var arr = new Float64Array( 10 ); 47 * var i; 48 * for ( i = 0; i < arr.length; i++ ) { 49 * arr[ i ] = randu(); 50 * } 51 * 52 * var opts = { 53 * 'sep': ',', 54 * 'objectMode': false, 55 * 'encoding': 'utf8', 56 * 'highWaterMark': 64 57 * }; 58 * 59 * var createStream = factory( opts ); 60 * 61 * // Create 10 identically configured streams... 62 * var streams = []; 63 * for ( i = 0; i < 10; i++ ) { 64 * streams.push( createStream( arr ) ); 65 * } 66 */ 67 function factory( options ) { 68 var opts; 69 if ( arguments.length ) { 70 opts = copy( options, 1 ); 71 } else { 72 opts = {}; 73 } 74 return createStream; 75 76 /** 77 * Returns a readable stream from a circular array-like object. 78 * 79 * @private 80 * @param {Collection} src - source array-like object 81 * @throws {TypeError} must provide an array-like object 82 * @throws {TypeError} options argument must be an object 83 * @throws {TypeError} must provide valid options 84 * @returns {CircularArrayStream} Stream instance 85 */ 86 function createStream( src ) { 87 return new CircularArrayStream( src, opts ); 88 } 89 } 90 91 92 // EXPORTS // 93 94 module.exports = factory;