README.md (5177B)
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 # Split Stream 22 23 > [Transform stream][transform-stream] which splits streamed data. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var splitStream = require( '@stdlib/streams/node/split' ); 31 ``` 32 33 <a name="split-stream"></a> 34 35 #### splitStream( \[options] ) 36 37 Creates a [transform stream][transform-stream] which splits streamed data. 38 39 ```javascript 40 var stdout = require( '@stdlib/streams/node/stdout' ); 41 42 var stream = splitStream(); 43 44 stream.pipe( stdout ); 45 stream.write( '1\n2\n3' ); 46 47 stream.end(); 48 49 // prints: 1 => 2 => 3 50 ``` 51 52 The function accepts the following `options`: 53 54 - **sep**: separator used to split streamed data. Similar to [`String#split`][string-split], a separator may be either a [regular expression][regexp] or a `string`. Default: `/\r?\n/`. 55 - **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`. 56 - **encoding**: specifies how `Buffer` objects should be decoded to `strings`. Default: `null`. 57 - **highWaterMark**: specifies the `Buffer` level at which `write()` calls start returning `false`. 58 - **allowHalfOpen**: specifies whether a [stream][stream] should remain open even if one side ends. Default: `false`. 59 - **writableObjectMode**: specifies whether the writable side should be in [objectMode][object-mode]. Default: `false`. 60 61 To set [stream][stream] `options`, 62 63 ```javascript 64 var opts = { 65 'sep': ',', 66 'objectMode': true, 67 'encoding': 'utf8', 68 'highWaterMark': 64, 69 'allowHalfOpen': true, 70 'writableObjectMode': false // overridden by `objectMode` option when `objectMode=true` 71 }; 72 73 var stream = splitStream( opts ); 74 ``` 75 76 #### splitStream.factory( \[options] ) 77 78 Returns a `function` for creating [streams][transform-stream] which are identically configured according to provided `options`. 79 80 ```javascript 81 var opts = { 82 'sep': '\t', 83 'objectMode': true, 84 'encoding': 'utf8', 85 'highWaterMark': 64 86 }; 87 88 var factory = splitStream.factory( opts ); 89 90 // Create 10 identically configured streams... 91 var streams = []; 92 var i; 93 for ( i = 0; i < 10; i++ ) { 94 streams.push( factory() ); 95 } 96 ``` 97 98 This method accepts the same `options` as [`splitStream()`](#split-stream). 99 100 #### splitStream.objectMode( \[options] ) 101 102 This method is a convenience function to create [streams][stream] which always operate in [objectMode][object-mode]. 103 104 ```javascript 105 var stdout = require( '@stdlib/streams/node/stdout' ); 106 107 var stream = splitStream.objectMode({ 108 'sep': ',' 109 }); 110 111 stream.pipe( stdout ); 112 stream.write( 'a,b,c' ); 113 114 stream.end(); 115 116 // prints: a => b => c 117 ``` 118 119 This method accepts the same `options` as [`splitStream()`](#split-stream); however, the method will **always** override the [objectMode][object-mode] option in `options`. 120 121 </section> 122 123 <!-- /.usage --> 124 125 <section class="notes"> 126 127 ## Notes 128 129 - Similar to [`String#split`][string-split], a separator which is a [regular expression][regexp] containing a matching group will result in the separator being retained in the output stream. 130 131 ```javascript 132 var stdout = require( '@stdlib/streams/node/stdout' ); 133 134 var stream = splitStream({ 135 'sep': /(,)/ 136 }); 137 138 stream.pipe( stdout ); 139 stream.write( '1,2,3' ); 140 141 stream.end(); 142 143 // prints: 1 => , => 2 => , => 3 144 ``` 145 146 </section> 147 148 <!-- /.notes --> 149 150 <section class="examples"> 151 152 ## Examples 153 154 <!-- eslint no-undef: "error" --> 155 156 ```javascript 157 var transformStream = require( '@stdlib/streams/node/transform' ); 158 var stdout = require( '@stdlib/streams/node/stdout' ); 159 var splitStream = require( '@stdlib/streams/node/split' ); 160 161 function append( chunk, enc, clbk ) { 162 clbk( null, chunk.toString()+'\n' ); 163 } 164 165 var newline; 166 var stream; 167 var i; 168 169 // Create a split stream to split tab delimited data: 170 stream = splitStream({ 171 'sep': /\t/ 172 }); 173 174 // Create a transform stream to make newline delimited data... 175 newline = transformStream({ 176 'transform': append, 177 'objectMode': true 178 }); 179 180 // Create a stream pipeline: 181 stream 182 .pipe( newline ) 183 .pipe( stdout ); 184 185 // Write data to the pipeline... 186 for ( i = 0; i < 10; i++ ) { 187 stream.write( i+'\t', 'utf8' ); 188 } 189 stream.end(); 190 ``` 191 192 </section> 193 194 <!-- /.examples --> 195 196 <section class="links"> 197 198 [stream]: https://nodejs.org/api/stream.html 199 200 [transform-stream]: https://nodejs.org/api/stream.html 201 202 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode 203 204 [string-split]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split 205 206 [regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 207 208 </section> 209 210 <!-- /.links -->