object_mode.js (2576B)
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 CircularArrayStream = require( './main.js' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an "objectMode" readable stream from an array-like object which repeatedly iterates over a provided value's elements. 32 * 33 * @param {Collection} src - source array-like object 34 * @param {Options} [options] - stream options 35 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings` 36 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of objects to store in an internal buffer before pausing streaming 37 * @param {NonNegativeInteger} [options.iter=1e308] - number of iterations 38 * @param {integer} [options.dir=1] - iteration direction 39 * @throws {TypeError} first argument must be an array-like object 40 * @throws {TypeError} options argument must be an object 41 * @throws {TypeError} must provide valid options 42 * @returns {CircularArrayStream} Stream instance 43 * 44 * @example 45 * var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 46 * var Float64Array = require( '@stdlib/array/float64' ); 47 * var randu = require( '@stdlib/random/base/randu' ); 48 * 49 * function log( v ) { 50 * console.log( v ); 51 * } 52 * 53 * var arr = new Float64Array( 10 ); 54 * var i; 55 * for ( i = 0; i < arr.length; i++ ) { 56 * arr[ i ] = randu(); 57 * } 58 * 59 * var opts = { 60 * 'iter': arr.length * 2 61 * }; 62 * 63 * var stream = objectMode( arr, opts ); 64 * 65 * stream.pipe( inspectStream.objectMode( log ) ); 66 */ 67 function objectMode( src, options ) { 68 var opts; 69 if ( arguments.length > 1 ) { 70 opts = options; 71 if ( !isObject( opts ) ) { 72 throw new TypeError( 'invalid argument. Options must be an object. Value: `' + opts + '`.' ); 73 } 74 opts = copy( options, 1 ); 75 } else { 76 opts = {}; 77 } 78 opts.objectMode = true; 79 return new CircularArrayStream( src, opts ); 80 } 81 82 83 // EXPORTS // 84 85 module.exports = objectMode;