time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

object_mode.js (2400B)


      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 Stream = require( './main.js' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an inspect stream with `objectMode` set to `true`.
     32 *
     33 * @param {Options} [options] - stream options
     34 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false`
     35 * @param {boolean} [options.allowHalfOpen=false] - specifies whether the stream should remain open even if one side ends
     36 * @param {boolean} [options.readableObjectMode=false] - specifies whether the readable side should be in object mode
     37 * @param {Callback} clbk - callback to invoke upon receiving data
     38 * @throws {TypeError} options argument must be an object
     39 * @throws {TypeError} must provide valid options
     40 * @throws {TypeError} must provide a callback function
     41 * @returns {InspectStream} inspect stream
     42 *
     43 * @example
     44 * function log( chunk, idx ) {
     45 *     console.log( 'index: %d', idx );
     46 *     console.log( chunk );
     47 * }
     48 *
     49 * var stream = objectMode( log );
     50 *
     51 * stream.write( {'value': 'a'} );
     52 * stream.write( {'value': 'b'} );
     53 * stream.write( {'value': 'c'} );
     54 *
     55 * stream.end();
     56 *
     57 * // prints: index: 0
     58 * // prints: {'value': 'a'}
     59 * // prints: index: 1
     60 * // prints: {'value': 'b'}
     61 * // prints: index: 2
     62 * // prints: {'value': 'c'}
     63 */
     64 function objectMode( options, clbk ) {
     65 	var opts;
     66 	var cb;
     67 	if ( arguments.length > 1 ) {
     68 		if ( !isObject( options ) ) {
     69 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     70 		}
     71 		opts = copy( options );
     72 		cb = clbk;
     73 	} else {
     74 		opts = {};
     75 		cb = options;
     76 	}
     77 	opts.objectMode = true;
     78 	return new Stream( opts, cb );
     79 }
     80 
     81 
     82 // EXPORTS //
     83 
     84 module.exports = objectMode;