object_mode.js (2696B)
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 having integer values. 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='mt19937'] - name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers 38 * @param {*} [options.seed] - pseudorandom number generator seed 39 * @param {*} [options.state] - pseudorandom number generator state 40 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state 41 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state 42 * @throws {TypeError} options argument must be an object 43 * @throws {TypeError} must provide valid options 44 * @throws {Error} must provide a valid state 45 * @returns {RandomStream} Stream instance 46 * 47 * @example 48 * var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 49 * 50 * function log( v ) { 51 * console.log( v ); 52 * } 53 * 54 * var opts = { 55 * 'iter': 10 56 * }; 57 * 58 * var stream = objectMode( opts ); 59 * 60 * stream.pipe( inspectStream.objectMode( log ) ); 61 */ 62 function objectMode( options ) { 63 var opts; 64 if ( arguments.length > 0 ) { 65 opts = options; 66 if ( !isObject( opts ) ) { 67 throw new TypeError( 'invalid argument. Options must be an object. Value: `' + opts + '`.' ); 68 } 69 opts = copy( options, 1 ); 70 } else { 71 opts = {}; 72 } 73 opts.objectMode = true; 74 return new RandomStream( opts ); 75 } 76 77 78 // EXPORTS // 79 80 module.exports = objectMode;