factory.js (3031B)
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 RandomStream = require( './main.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a function for creating readable streams which generate pseudorandom numbers via a linear congruential pseudorandom number generator (LCG) based on Park and Miller. 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 ceasing to generate additional pseudorandom numbers 36 * @param {string} [options.sep='\n'] - separator used to join streamed data 37 * @param {NonNegativeInteger} [options.iter] - number of iterations 38 * @param {boolean} [options.normalized=false] - boolean indicating whether to return pseudorandom numbers on the interval `[0,1)` 39 * @param {PRNGSeedMINSTD} [options.seed] - pseudorandom number generator seed 40 * @param {PRNGStateMINSTD} [options.state] - pseudorandom number generator state 41 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state 42 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state 43 * @returns {Function} stream factory 44 * 45 * @example 46 * var opts = { 47 * 'sep': ',', 48 * 'objectMode': false, 49 * 'encoding': 'utf8', 50 * 'highWaterMark': 64 51 * }; 52 * 53 * var createStream = factory( opts ); 54 * 55 * // Create 10 identically configured streams... 56 * var streams = []; 57 * var i; 58 * for ( i = 0; i < 10; i++ ) { 59 * streams.push( createStream() ); 60 * } 61 */ 62 function factory( options ) { 63 var opts; 64 if ( arguments.length > 0 ) { 65 opts = copy( options, 1 ); 66 } else { 67 opts = {}; 68 } 69 return createStream; 70 71 /** 72 * Returns a readable stream for generating pseudorandom numbers via a linear congruential pseudorandom number generator (LCG) based on Park and Miller. 73 * 74 * @private 75 * @throws {TypeError} options argument must be an object 76 * @throws {TypeError} must provide valid options 77 * @throws {Error} must provide a valid state 78 * @returns {RandomStream} Stream instance 79 */ 80 function createStream() { 81 return new RandomStream( opts ); 82 } 83 } 84 85 86 // EXPORTS // 87 88 module.exports = factory;