time-to-botec

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

validate.js (3469B)


      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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     26 var isNonNegative = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive;
     27 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Validates function options.
     34 *
     35 * @private
     36 * @param {Object} opts - destination object
     37 * @param {Options} options - function options
     38 * @param {string} [options.name] - debug namespace
     39 * @param {boolean} [options.objectMode] - specifies whether a stream should operate in object mode
     40 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false`
     41 * @param {boolean} [options.decodeStrings] - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream
     42 * @param {string} [options.defaultEncoding] - default encoding when not explicitly specified when writing data
     43 * @returns {(Error|null)} null or an error object
     44 *
     45 * @example
     46 * var opts = {};
     47 * var options = {
     48 *     'objectMode': true
     49 * };
     50 *
     51 * var err = validate( opts, options );
     52 * if ( err ) {
     53 *     throw err;
     54 * }
     55 */
     56 function validate( opts, options ) {
     57 	if ( !isObject( options ) ) {
     58 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     59 	}
     60 	if ( hasOwnProp( options, 'name' ) ) {
     61 		opts.name = options.name;
     62 		if ( !isString( opts.name ) ) {
     63 			return new TypeError( 'invalid option. `name` option must be a primitive string. Option: `' + opts.name + '`.' );
     64 		}
     65 	}
     66 	if ( hasOwnProp( options, 'objectMode' ) ) {
     67 		opts.objectMode = options.objectMode;
     68 		if ( !isBoolean( opts.objectMode ) ) {
     69 			return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' );
     70 		}
     71 	}
     72 	if ( hasOwnProp( options, 'highWaterMark' ) ) {
     73 		opts.highWaterMark = options.highWaterMark;
     74 		if ( !isNonNegative( opts.highWaterMark ) ) {
     75 			return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' );
     76 		}
     77 	}
     78 	if ( hasOwnProp( options, 'decodeStrings' ) ) {
     79 		opts.decodeStrings = options.decodeStrings;
     80 		if ( !isBoolean( opts.decodeStrings ) ) {
     81 			return new TypeError( 'invalid option. `decodeStrings` option must be a primitive boolean. Option: `' + opts.decodeStrings + '`.' );
     82 		}
     83 	}
     84 	if ( hasOwnProp( options, 'defaultEncoding' ) ) {
     85 		opts.defaultEncoding = options.defaultEncoding;
     86 		if ( !isString( opts.defaultEncoding ) ) {
     87 			return new TypeError( 'invalid option. `defaultEncoding` option must be a primitive string. Option: `' + opts.defaultEncoding + '`.' );
     88 		}
     89 	}
     90 	return null;
     91 }
     92 
     93 
     94 // EXPORTS //
     95 
     96 module.exports = validate;