object_mode.js (2877B)
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 RandomStream = require( './main.js' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an "objectMode" readable stream for generating pseudorandom numbers drawn from a standard normal distribution. 32 * 33 * @param {Options} [options] - stream options 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 objects to store in an internal buffer before ceasing to generate additional pseudorandom numbers 36 * @param {NonNegativeInteger} [options.iter] - number of iterations 37 * @param {string} [options.name='improved-ziggurat'] - name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers 38 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers 39 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed 40 * @param {PRNGStateMT19937} [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 * @throws {TypeError} options argument must be an object 44 * @throws {TypeError} must provide valid options 45 * @throws {Error} must provide a valid state 46 * @returns {RandomStream} Stream instance 47 * 48 * @example 49 * var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 50 * 51 * function log( v ) { 52 * console.log( v ); 53 * } 54 * 55 * var opts = { 56 * 'iter': 10 57 * }; 58 * 59 * var stream = objectMode( opts ); 60 * 61 * stream.pipe( inspectStream.objectMode( log ) ); 62 */ 63 function objectMode( options ) { 64 var opts; 65 if ( arguments.length > 0 ) { 66 opts = options; 67 if ( !isObject( opts ) ) { 68 throw new TypeError( 'invalid argument. Options must be an object. Value: `' + opts + '`.' ); 69 } 70 opts = copy( options, 1 ); 71 } else { 72 opts = {}; 73 } 74 opts.objectMode = true; 75 return new RandomStream( opts ); 76 } 77 78 79 // EXPORTS // 80 81 module.exports = objectMode;