README.md (5487B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Inspect Stream 22 23 > [Writable stream][writable-stream] for inspecting streamed data. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var inspectSinkStream = require( '@stdlib/streams/node/inspect-sink' ); 31 ``` 32 33 <a name="inspect-sink-stream"></a> 34 35 #### inspectSinkStream( \[options,] clbk ) 36 37 Creates a [writable stream][writable-stream] for inspecting streamed data. 38 39 ```javascript 40 function log( chunk, idx ) { 41 console.log( 'index: %d', idx ); 42 console.log( chunk ); 43 } 44 45 var stream = inspectSinkStream( log ); 46 47 stream.write( 'a' ); 48 stream.write( 'b' ); 49 stream.write( 'c' ); 50 51 stream.end(); 52 53 // prints: index: 0 54 // prints: a 55 // prints: index: 1 56 // prints: b 57 // prints: index: 2 58 // prints: c 59 ``` 60 61 The function accepts the following `options`: 62 63 - **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`. 64 - **highWaterMark**: specifies the `Buffer` level at which `write()` calls start returning `false`. 65 - **decodeStrings**: specifies whether to encode strings as `Buffer` objects before writing data to a returned [stream][stream]. Default: `true`. 66 - **defaultEncoding**: default encoding when not explicitly specified when writing data. Default: `'utf8'`. 67 68 To set [stream][stream] `options`, 69 70 ```javascript 71 function log( chunk, idx ) { 72 console.log( 'index: %d', idx ); 73 console.log( chunk ); 74 } 75 76 var opts = { 77 'objectMode': true, 78 'highWaterMark': 64, 79 'decodeStrings': false, 80 'defaultEncoding': 'utf8' 81 }; 82 83 var stream = inspectSinkStream( opts, log ); 84 ``` 85 86 #### inspectSinkStream.factory( \[options] ) 87 88 Returns a `function` for creating [streams][writable-stream] which are identically configured according to provided `options`. 89 90 ```javascript 91 var opts = { 92 'objectMode': true, 93 'highWaterMark': 64 94 }; 95 96 var factory = inspectSinkStream.factory( opts ); 97 ``` 98 99 This method accepts the same `options` as [`inspectSinkStream()`](#inspect-sink-stream). 100 101 ##### factory( clbk ) 102 103 Creates a [writable stream][writable-stream] for inspecting streamed data. 104 105 ```javascript 106 function log( chunk, idx ) { 107 console.log( 'index: %d', idx ); 108 console.log( chunk ); 109 } 110 111 var factory = inspectSinkStream.factory(); 112 113 // Create 10 identically configured streams... 114 var streams = []; 115 var i; 116 for ( i = 0; i < 10; i++ ) { 117 streams.push( factory( log ) ); 118 } 119 ``` 120 121 #### inspectSinkStream.objectMode( \[options,] clbk ) 122 123 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode]. 124 125 <!-- eslint-disable object-curly-newline --> 126 127 ```javascript 128 function log( chunk, idx ) { 129 console.log( 'index: %d', idx ); 130 console.log( chunk ); 131 } 132 133 var stream = inspectSinkStream.objectMode( log ); 134 135 stream.write( { 'value': 'a' } ); 136 stream.write( { 'value': 'b' } ); 137 stream.write( { 'value': 'c' } ); 138 139 stream.end(); 140 141 // prints: index: 0 142 // prints: {'value': 'a'} 143 // prints: index: 1 144 // prints: {'value': 'b'} 145 // prints: index: 2 146 // prints: {'value': 'c'} 147 ``` 148 149 This method accepts the same `options` as [`inspectSinkStream()`](#inspect-sink-stream); however, the method will **always** override the [objectMode][object-mode] option in `options`. 150 151 </section> 152 153 <!-- /.usage --> 154 155 <section class="examples"> 156 157 ## Examples 158 159 <!-- eslint no-undef: "error" --> 160 161 ```javascript 162 var parseJSON = require( '@stdlib/utils/parse-json' ); 163 var transformFactory = require( '@stdlib/streams/node/transform' ).factory; 164 var inspect = require( '@stdlib/streams/node/inspect-sink' ).objectMode; 165 166 function parse( chunk, enc, clbk ) { 167 clbk( null, parseJSON( chunk ) ); 168 } 169 170 function pluck( chunk, enc, clbk ) { 171 clbk( null, chunk.value ); 172 } 173 174 function square( chunk, enc, clbk ) { 175 var v = +chunk; 176 clbk( null, v*v ); 177 } 178 179 function toStr( chunk, enc, clbk ) { 180 clbk( null, chunk.toString() ); 181 } 182 183 function join( chunk, enc, clbk ) { 184 clbk( null, chunk+'\n' ); 185 } 186 187 function logger( chunk, idx ) { 188 console.log( 'index: %d', idx ); 189 console.log( chunk ); 190 } 191 192 // Create a factory for generating streams running in `objectMode`: 193 var tStream = transformFactory({ 194 'objectMode': true 195 }); 196 197 // Create streams for each transform: 198 var s1 = tStream( parse ); 199 var s2 = tStream( pluck ); 200 var s3 = tStream( square ); 201 var s4 = tStream( toStr ); 202 var s5 = tStream( join ); 203 204 // Create a writable stream for inspecting the result of the transformations: 205 var is = inspect( logger ); 206 207 // Create the pipeline: 208 s1.pipe( s2 ) 209 .pipe( s3 ) 210 .pipe( s4 ) 211 .pipe( s5 ) 212 .pipe( is ); 213 214 // Write data to the pipeline... 215 var v; 216 var i; 217 for ( i = 0; i < 100; i++ ) { 218 v = '{"value":'+i+'}'; 219 s1.write( v, 'utf8' ); 220 } 221 s1.end(); 222 ``` 223 224 </section> 225 226 <!-- /.examples --> 227 228 <section class="links"> 229 230 [stream]: https://nodejs.org/api/stream.html 231 232 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode 233 234 [writable-stream]: https://nodejs.org/api/stream.html 235 236 </section> 237 238 <!-- /.links -->