object_mode.js (2780B)
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 isFunction = require( '@stdlib/assert/is-function' ); 24 var isObject = require( '@stdlib/assert/is-plain-object' ); 25 var copy = require( '@stdlib/utils/copy' ); 26 var Stream = require( './main.js' ); 27 28 29 // MAIN // 30 31 /** 32 * Returns a debug stream with `objectMode` set to `true`. 33 * 34 * @param {Options} [options] - stream options 35 * @param {string} [options.name] - debug namespace 36 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false` 37 * @param {boolean} [options.decodeStrings=true] - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream 38 * @param {string} [options.defaultEncoding='utf8'] - default encoding when not explicitly specified when writing data 39 * @param {Callback} [clbk] - callback to invoke upon receiving data 40 * @throws {TypeError} options argument must be an object 41 * @throws {TypeError} must provide valid options 42 * @throws {TypeError} must provide a valid callback argument 43 * @returns {DebugSinkStream} debug stream 44 * 45 * @example 46 * var stream = objectMode({ 47 * 'name': 'my-stream' 48 * }); 49 * 50 * stream.write( {'value': 'a'} ); 51 * stream.write( {'value': 'b'} ); 52 * stream.write( {'value': 'c'} ); 53 * stream.end(); 54 */ 55 function objectMode( options, clbk ) { 56 var opts; 57 var cb; 58 if ( arguments.length > 1 ) { 59 if ( !isObject( options ) ) { 60 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 61 } 62 opts = copy( options ); 63 cb = clbk; 64 if ( !isFunction( clbk ) ) { 65 throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + clbk + '`.' ); 66 } 67 } else if ( arguments.length ) { 68 if ( isFunction( options ) ) { 69 opts = {}; 70 cb = options; 71 } else { 72 if ( !isObject( options ) ) { 73 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 74 } 75 opts = copy( options ); 76 } 77 } else { 78 opts = {}; 79 } 80 opts.objectMode = true; 81 if ( cb === void 0 ) { 82 return new Stream( opts ); 83 } 84 return new Stream( opts, cb ); 85 } 86 87 88 // EXPORTS // 89 90 module.exports = objectMode;