factory.js (4134B)
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 drawn from a Fréchet distribution. 31 * 32 * @param {PositiveNumber} [alpha] - shape parameter 33 * @param {PositiveNumber} [s] - scale parameter 34 * @param {number} [m] - location parameter 35 * @param {Options} [options] - stream options 36 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode 37 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings` 38 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in an internal buffer before ceasing to generate additional pseudorandom numbers 39 * @param {string} [options.sep='\n'] - separator used to join streamed data 40 * @param {NonNegativeInteger} [options.iter] - number of iterations 41 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers 42 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed 43 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state 44 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state 45 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state 46 * @returns {Function} stream factory 47 * 48 * @example 49 * var opts = { 50 * 'sep': ',', 51 * 'objectMode': false, 52 * 'encoding': 'utf8', 53 * 'highWaterMark': 64 54 * }; 55 * 56 * var createStream = factory( opts ); 57 * 58 * // Create 10 identically configured streams... 59 * var streams = []; 60 * var i; 61 * for ( i = 0; i < 10; i++ ) { 62 * streams.push( createStream( 2.0, 5.0, 0.0 ) ); 63 * } 64 */ 65 function factory( alpha, s, m, options ) { 66 var nargs; 67 var opts; 68 var fcn; 69 70 nargs = arguments.length; 71 if ( nargs === 1 ) { 72 opts = copy( alpha, 1 ); 73 } else if ( nargs > 3 ) { 74 opts = copy( options, 1 ); 75 } else { 76 opts = {}; 77 } 78 if ( nargs < 3 ) { 79 fcn = createStream1; 80 } else { 81 fcn = createStream2; 82 } 83 return fcn; 84 85 /** 86 * Returns a readable stream for generating pseudorandom numbers drawn from a Fréchet distribution. 87 * 88 * @private 89 * @param {PositiveNumber} alpha - shape parameter 90 * @param {PositiveNumber} s - scale parameter 91 * @param {number} m - location parameter 92 * @throws {TypeError} `alpha` must be a positive number 93 * @throws {TypeError} `s` must be a positive number 94 * @throws {TypeError} `m` must be a number 95 * @throws {TypeError} options argument must be an object 96 * @throws {TypeError} must provide valid options 97 * @throws {Error} must provide a valid state 98 * @returns {RandomStream} Stream instance 99 */ 100 function createStream1( alpha, s, m ) { 101 return new RandomStream( alpha, s, m, opts ); 102 } 103 104 /** 105 * Returns a readable stream for generating pseudorandom numbers drawn from a Fréchet distribution. 106 * 107 * @private 108 * @throws {TypeError} `alpha` must be a positive number 109 * @throws {TypeError} `s` must be a positive number 110 * @throws {TypeError} `m` must be a number 111 * @throws {TypeError} options argument must be an object 112 * @throws {TypeError} must provide valid options 113 * @throws {Error} must provide a valid state 114 * @returns {RandomStream} Stream instance 115 */ 116 function createStream2() { 117 return new RandomStream( alpha, s, m, opts ); 118 } 119 } 120 121 122 // EXPORTS // 123 124 module.exports = factory;