time-to-botec

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

README.md (6058B)


      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 # Array Stream
     22 
     23 > Create a [readable stream][readable-stream] from an array-like object.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var arrayStream = require( '@stdlib/streams/node/from-array' );
     31 ```
     32 
     33 <a name="array-stream"></a>
     34 
     35 #### arrayStream( src\[, options] )
     36 
     37 Returns a [readable stream][readable-stream] from an array-like `object`.
     38 
     39 ```javascript
     40 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     41 
     42 function log( chunk ) {
     43     console.log( chunk.toString() );
     44 }
     45 
     46 var stream = arrayStream( [ 1, 2, 3, 4 ] );
     47 var iStream = inspectStream( log );
     48 
     49 stream.pipe( iStream );
     50 ```
     51 
     52 The function accepts the following `options`:
     53 
     54 -   **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`.
     55 -   **encoding**: specifies how `Buffer` objects should be decoded to `strings`. Default: `null`.
     56 -   **highWaterMark**: specifies the maximum number of bytes to store in an internal buffer before pausing streaming.
     57 -   **sep**: separator used to join streamed data. This option is only applicable when a stream is **not** in [objectMode][object-mode]. Default: `'\n'`.
     58 -   **serialize**: custom serialization function. This option is only applicable when a stream is **not** in [objectMode][object-mode].
     59 -   **dir**: iteration direction. If set to `-1`, the stream iterates over elements from right-to-left. Default: `1`.
     60 
     61 To set [stream][stream] `options`,
     62 
     63 ```javascript
     64 var opts = {
     65     'objectMode': true,
     66     'encoding': 'utf8',
     67     'highWaterMark': 64
     68 };
     69 
     70 var stream = arrayStream( [ 1, 2, 3, 4 ], opts );
     71 ```
     72 
     73 By default, when not operating in [objectMode][object-mode], a returned [stream][stream] delineates individual values using a newline character. To specify an alternative separator, set the `sep` option.
     74 
     75 ```javascript
     76 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     77 
     78 function log( chunk ) {
     79     console.log( chunk.toString() );
     80 }
     81 
     82 var stream = arrayStream( [ 1, 2, 3, 4 ], {
     83     'sep': ','
     84 });
     85 
     86 var iStream = inspectStream( log );
     87 
     88 stream.pipe( iStream );
     89 ```
     90 
     91 By default, when not operating in [objectMode][object-mode], a returned [stream][stream] serializes values as JSON strings. To specify custom serialization behavior (either to a `string` or `Buffer`), set the `serialize` option.
     92 
     93 ```javascript
     94 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     95 
     96 function serialize( v ) {
     97     return 'v::' + v.toString();
     98 }
     99 
    100 function log( chunk ) {
    101     console.log( chunk.toString() );
    102 }
    103 
    104 var stream = arrayStream( [ 1, 2, 3, 4 ], {
    105     'serialize': serialize
    106 });
    107 
    108 var iStream = inspectStream( log );
    109 
    110 stream.pipe( iStream );
    111 ```
    112 
    113 * * *
    114 
    115 #### arrayStream.factory( \[options] )
    116 
    117 Returns a `function` for creating [readable streams][readable-stream] from array-like objects.
    118 
    119 ```javascript
    120 var opts = {
    121     'objectMode': true,
    122     'encoding': 'utf8',
    123     'highWaterMark': 64
    124 };
    125 
    126 var createStream = arrayStream.factory( opts );
    127 
    128 var stream1 = createStream( [ 1, 2, 3, 4 ] );
    129 var stream2 = createStream( [ 5, 6, 7, 8 ] );
    130 // ...
    131 ```
    132 
    133 The method accepts the same `options` as [`arrayStream()`](#array-stream).
    134 
    135 * * *
    136 
    137 #### arrayStream.objectMode( src\[, options] )
    138 
    139 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode].
    140 
    141 ```javascript
    142 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    143 
    144 function log( v ) {
    145     console.log( v );
    146 }
    147 
    148 var stream = arrayStream.objectMode( [ 1, 2, 3, 4 ] );
    149 
    150 var opts = {
    151     'objectMode': true
    152 };
    153 var iStream = inspectStream( opts, log );
    154 
    155 stream.pipe( iStream );
    156 ```
    157 
    158 This method accepts the same `options` as [`arrayStream()`](#array-stream); however, the method will **always** override the [`objectMode`][object-mode] option in `options`.
    159 
    160 </section>
    161 
    162 <!-- /.usage -->
    163 
    164 * * *
    165 
    166 <section class="notes">
    167 
    168 ## Notes
    169 
    170 -   In [`objectMode`][object-mode], `null` is a reserved value. If an `array` contains `null` values (e.g., as a means to encode missing values), the stream will prematurely end. Consider an alternative encoding or filter `null` values prior to invocation.
    171 -   In binary mode, if an `array` contains `undefined` values, the stream will emit an error. Consider providing a custom serialization function or filtering `undefined` values prior to invocation.
    172 
    173 </section>
    174 
    175 <!-- /.notes -->
    176 
    177 * * *
    178 
    179 <section class="examples">
    180 
    181 ## Examples
    182 
    183 <!-- eslint no-undef: "error" -->
    184 
    185 ```javascript
    186 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    187 var randu = require( '@stdlib/random/base/randu' );
    188 var Float64Array = require( '@stdlib/array/float64' );
    189 var arrayStream = require( '@stdlib/streams/node/from-array' );
    190 
    191 function log( v ) {
    192     console.log( v.toString() );
    193 }
    194 
    195 // Create an array containing uniformly distributed pseudorandom numbers:
    196 var arr = new Float64Array( 10 );
    197 
    198 var i;
    199 for ( i = 0; i < arr.length; i++ ) {
    200     arr[ i ] = randu();
    201 }
    202 
    203 // Convert the array to a stream:
    204 var opts = {
    205     'objectMode': true
    206 };
    207 var stream = arrayStream( arr, opts );
    208 
    209 // Create a writable stream for inspecting stream data:
    210 opts = {
    211     'objectMode': true
    212 };
    213 var iStream = inspectStream( opts, log );
    214 
    215 // Begin data flow:
    216 stream.pipe( iStream );
    217 ```
    218 
    219 </section>
    220 
    221 <!-- /.examples -->
    222 
    223 <section class="links">
    224 
    225 [stream]: https://nodejs.org/api/stream.html
    226 
    227 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode
    228 
    229 [readable-stream]: https://nodejs.org/api/stream.html
    230 
    231 </section>
    232 
    233 <!-- /.links -->