time-to-botec

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

validate.js (3725B)


      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 var isFunction = require( '@stdlib/assert/is-function' );
     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 {string} [options.sep] - separator used to join streamed data
     40 * @param {boolean} [options.objectMode] - specifies whether a stream should operate in object mode
     41 * @param {(string|null)} [options.encoding] - specifies how `Buffer` objects should be decoded to `strings`
     42 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in the internal buffer before pausing streaming
     43 * @param {Function} [options.serialize] - custom serialization function
     44 * @param {integer} [options.dir] - iteration direction
     45 * @returns {(Error|null)} null or an error object
     46 *
     47 * @example
     48 * var opts = {};
     49 * var options = {
     50 *     'objectMode': true
     51 * };
     52 * var err= validate( opts, options );
     53 * if ( err ) {
     54 *     throw err;
     55 * }
     56 */
     57 function validate( opts, options ) {
     58 	if ( !isObject( options ) ) {
     59 		return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
     60 	}
     61 	if ( hasOwnProp( options, 'sep' ) ) {
     62 		opts.sep = options.sep;
     63 		if ( !isString( opts.sep ) ) {
     64 			return new TypeError( 'invalid option. `sep` option must be a primitive string. Option: `' + opts.sep + '`.' );
     65 		}
     66 	}
     67 	if ( hasOwnProp( options, 'objectMode' ) ) {
     68 		opts.objectMode = options.objectMode;
     69 		if ( !isBoolean( opts.objectMode ) ) {
     70 			return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' );
     71 		}
     72 	}
     73 	if ( hasOwnProp( options, 'encoding' ) ) {
     74 		opts.encoding = options.encoding;
     75 		if ( !isString( opts.encoding ) && opts.encoding !== null ) {
     76 			return new TypeError( 'invalid option. `encoding` option must be a primitive string or null. Option: `' + opts.encoding + '`.' );
     77 		}
     78 	}
     79 	if ( hasOwnProp( options, 'highWaterMark' ) ) {
     80 		opts.highWaterMark = options.highWaterMark;
     81 		if ( !isNonNegative( opts.highWaterMark ) ) {
     82 			return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' );
     83 		}
     84 	}
     85 	if ( hasOwnProp( options, 'serialize' ) ) {
     86 		opts.serialize = options.serialize;
     87 		if ( !isFunction( opts.serialize ) ) {
     88 			return new TypeError( 'invalid option. `serialize` option must be a function. Option: `' + opts.serialize + '`.' );
     89 		}
     90 	}
     91 	if ( hasOwnProp( options, 'dir' ) ) {
     92 		opts.dir = options.dir;
     93 		if ( opts.dir !== 1 && opts.dir !== -1 ) {
     94 			return new TypeError( 'invalid option. `dir` option must be either `1` or `-1`. Option: `' + opts.dir + '`.' );
     95 		}
     96 	}
     97 	return null;
     98 }
     99 
    100 
    101 // EXPORTS //
    102 
    103 module.exports = validate;