time-to-botec

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

object_mode.js (2727B)


      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 StridedArrayStream = require( './main.js' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Returns an "objectMode" readable stream from an array-like value.
     32 *
     33 * @param {NonNegativeInteger} N - number of values to stream
     34 * @param {Collection} buffer - source value
     35 * @param {integer} stride - stride length
     36 * @param {NonNegativeInteger} offset - starting index
     37 * @param {Options} [options] - stream options
     38 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings`
     39 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of objects to store in an internal buffer before pausing streaming
     40 * @throws {TypeError} first argument must be a nonnegative integer
     41 * @throws {TypeError} second argument must be an array-like object
     42 * @throws {TypeError} third argument must be an integer
     43 * @throws {TypeError} fourth argument must be a nonnegative integer
     44 * @throws {TypeError} options argument must be an object
     45 * @throws {TypeError} must provide valid options
     46 * @returns {StridedArrayStream} Stream instance
     47 *
     48 * @example
     49 * var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     50 * var Float64Array = require( '@stdlib/array/float64' );
     51 * var randu = require( '@stdlib/random/base/randu' );
     52 *
     53 * function log( v ) {
     54 *    console.log( v );
     55 * }
     56 *
     57 * var arr = new Float64Array( 10 );
     58 * var i;
     59 * for ( i = 0; i < arr.length; i++ ) {
     60 *     arr[ i ] = randu();
     61 * }
     62 *
     63 * var stream = objectMode( arr.length, arr, 1, 0 );
     64 *
     65 * stream.pipe( inspectStream.objectMode( log ) );
     66 */
     67 function objectMode( N, buffer, stride, offset, options ) {
     68 	var opts;
     69 	if ( arguments.length > 4 ) {
     70 		opts = options;
     71 		if ( !isObject( opts ) ) {
     72 			throw new TypeError( 'invalid argument. Options must be an object. Value: `' + opts + '`.' );
     73 		}
     74 		opts = copy( options, 1 );
     75 	} else {
     76 		opts = {};
     77 	}
     78 	opts.objectMode = true;
     79 	return new StridedArrayStream( N, buffer, stride, offset, opts );
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = objectMode;