time-to-botec

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

validate.js (3302B)


      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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     26 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     27 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     28 var isNull = require( '@stdlib/assert/is-null' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Validates function options.
     35 *
     36 * @private
     37 * @param {Object} opts - destination object
     38 * @param {Options} options - options to validate
     39 * @param {string} [options.before] - setup code
     40 * @param {string} [options.after] - cleanup code
     41 * @param {(PositiveInteger|null)} [options.iterations] - number of iterations
     42 * @param {PositiveInteger} [options.repeats] - number of repeats
     43 * @param {boolean} [options.asynchronous] - boolean indicating whether a snippet is asynchronous
     44 * @returns {(Error|null)} error or null
     45 *
     46 * @example
     47 * var opts = {};
     48 * var options = {
     49 *     'iterations': 1e7,
     50 *     'repeats': 5
     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 argument must be an object. Value: `' + options + '`.' );
     60 	}
     61 	if ( hasOwnProp( options, 'iterations' ) ) {
     62 		opts.iterations = options.iterations;
     63 		if (
     64 			!isPositiveInteger( opts.iterations ) &&
     65 			!isNull( opts.iterations )
     66 		) {
     67 			return new TypeError( 'invalid option. `iterations` option must be a positive integer or null. Option: `' + opts.iterations + '`.' );
     68 		}
     69 	}
     70 	if ( hasOwnProp( options, 'repeats' ) ) {
     71 		opts.repeats = options.repeats;
     72 		if ( !isPositiveInteger( opts.repeats ) ) {
     73 			return new TypeError( 'invalid option. `repeats` option must be a positive integer. Option: `' + opts.repeats + '`.' );
     74 		}
     75 	}
     76 	if ( hasOwnProp( options, 'before' ) ) {
     77 		opts.before = options.before;
     78 		if ( !isString( opts.before ) ) {
     79 			return new TypeError( 'invalid option. `before` option must be a primitive string. Option: `' + opts.before + '`.' );
     80 		}
     81 	}
     82 	if ( hasOwnProp( options, 'after' ) ) {
     83 		opts.after = options.after;
     84 		if ( !isString( opts.after ) ) {
     85 			return new TypeError( 'invalid option. `after` option must be a primitive string. Option: `' + opts.after + '`.' );
     86 		}
     87 	}
     88 	if ( hasOwnProp( options, 'asynchronous' ) ) {
     89 		opts.asynchronous = options.asynchronous;
     90 		if ( !isBoolean( opts.asynchronous ) ) {
     91 			return new TypeError( 'invalid option. `asynchronous` option must be a primitive boolean. Option: `' + opts.asynchronous + '`.' );
     92 		}
     93 	}
     94 	return null;
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = validate;