time-to-botec

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

validate.js (4094B)


      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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
     25 var isFunction = require( '@stdlib/assert/is-function' );
     26 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     27 var isNonNegative = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive;
     28 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Validates function options.
     35 *
     36 * @private
     37 * @param {Object} opts - destination object
     38 * @param {Options} options - function options
     39 * @param {Function} [options.transform] - callback to invoke upon receiving a new chunk
     40 * @param {Function} [options.flush] - callback to invoke after receiving all chunks and prior to the stream closing
     41 * @param {boolean} [options.objectMode] - specifies whether a stream should operate in object mode
     42 * @param {(string|null)} [options.encoding] - specifies how `Buffer` objects should be decoded to `strings`
     43 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false`
     44 * @param {boolean} [options.allowHalfOpen] - specifies whether the stream should remain open even if one side ends
     45 * @param {boolean} [options.decodeStrings] - specifies whether to decode `strings` into `Buffer` objects when writing
     46 * @returns {(Error|null)} null or an error object
     47 */
     48 function validate( opts, options ) {
     49 	if ( !isObject( options ) ) {
     50 		return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
     51 	}
     52 	if ( hasOwnProp( options, 'transform' ) ) {
     53 		opts.transform = options.transform;
     54 		if ( !isFunction( opts.transform ) ) {
     55 			return new TypeError( 'invalid option. `transform` option must be a function. Option: `' + opts.transform + '`.' );
     56 		}
     57 	}
     58 	if ( hasOwnProp( options, 'flush' ) ) {
     59 		opts.flush = options.flush;
     60 		if ( !isFunction( opts.flush ) ) {
     61 			return new TypeError( 'invalid option. `flush` option must be a function. Option: `' + opts.flush + '`.' );
     62 		}
     63 	}
     64 	if ( hasOwnProp( options, 'objectMode' ) ) {
     65 		opts.objectMode = options.objectMode;
     66 		if ( !isBoolean( opts.objectMode ) ) {
     67 			return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' );
     68 		}
     69 	}
     70 	if ( hasOwnProp( options, 'encoding' ) ) {
     71 		opts.encoding = options.encoding;
     72 		if ( !isString( opts.encoding ) ) {
     73 			return new TypeError( 'invalid option. `encoding` option must be a primitive string. Option: `' + opts.encoding + '`.' );
     74 		}
     75 	}
     76 	if ( hasOwnProp( options, 'allowHalfOpen' ) ) {
     77 		opts.allowHalfOpen = options.allowHalfOpen;
     78 		if ( !isBoolean( opts.allowHalfOpen ) ) {
     79 			return new TypeError( 'invalid option. `allowHalfOpen` option must be a primitive boolean. Option: `' + opts.allowHalfOpen + '`.' );
     80 		}
     81 	}
     82 	if ( hasOwnProp( options, 'highWaterMark' ) ) {
     83 		opts.highWaterMark = options.highWaterMark;
     84 		if ( !isNonNegative( opts.highWaterMark ) ) {
     85 			return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' );
     86 		}
     87 	}
     88 	if ( hasOwnProp( options, 'decodeStrings' ) ) {
     89 		opts.decodeStrings = options.decodeStrings;
     90 		if ( !isBoolean( opts.decodeStrings ) ) {
     91 			return new TypeError( 'invalid option. `decodeStrings` option must be a primitive boolean. Option: `' + opts.decodeStrings + '`.' );
     92 		}
     93 	}
     94 	return null;
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = validate;