time-to-botec

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

validate.js (3303B)


      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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     24 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     25 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     26 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     27 var isObject = require( '@stdlib/assert/is-plain-object' );
     28 var isnan = require( '@stdlib/assert/is-nan' );
     29 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Validates function options.
     36 *
     37 * @private
     38 * @param {Object} opts - destination for validated options
     39 * @param {Options} options - function options
     40 * @param {number} [options.alpha] - significance level
     41 * @param {NonNegativeInteger} [options.ddof] - degrees of freedom adjustment
     42 * @param {boolean} [options.simulate] - boolean indicating whether to compute p-values by Monte Carlo simulation
     43 * @param {PositiveInteger} [options.iterations] - number of Monte Carlo iterations
     44 * @returns {(null|Error)} null or an error
     45 *
     46 * @example
     47 * var opts = {};
     48 * var options = {
     49 *     'alpha': 0.01,
     50 *     'simulate': false,
     51 *     'ddof': 2
     52 * };
     53 * var err = validate( opts, options );
     54 * if ( err ) {
     55 *     throw err;
     56 * }
     57 */
     58 function validate( opts, options ) {
     59 	if ( !isObject( options ) ) {
     60 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     61 	}
     62 	if ( hasOwnProp( options, 'alpha' ) ) {
     63 		opts.alpha = options.alpha;
     64 		if ( !isNumber( opts.alpha ) || isnan( opts.alpha ) ) {
     65 			return new TypeError( 'invalid option. `alpha` option must be a number primitive. Option: `' + opts.alpha + '`.' );
     66 		}
     67 		if ( opts.alpha < 0.0 || opts.alpha > 1.0 ) {
     68 			return new RangeError( 'invalid option. `alpha` option must be a number on the interval `[0,1]`. Value: `' + opts.alpha + '`.' );
     69 		}
     70 	}
     71 	if ( hasOwnProp( options, 'ddof' ) ) {
     72 		opts.ddof = options.ddof;
     73 		if ( !isNonNegativeInteger( opts.ddof ) ) {
     74 			return new TypeError( 'invalid option. `ddof` option must be a nonnegative integer. Option: `' + opts.ddof + '`.' );
     75 		}
     76 	}
     77 	if ( hasOwnProp( options, 'iterations' ) ) {
     78 		opts.iterations = options.iterations;
     79 		if ( !isPositiveInteger( opts.iterations ) ) {
     80 			return new TypeError( 'invalid option. `iterations` option must be a positive integer. Option: `' + opts.iterations + '`.' );
     81 		}
     82 	}
     83 	if ( hasOwnProp( options, 'simulate' ) ) {
     84 		opts.simulate = options.simulate;
     85 		if ( !isBoolean( opts.simulate ) ) {
     86 			return new TypeError( 'invalid option. `simulate` option must be a boolean primitive. Option: `' + opts.simulate + '`.' );
     87 		}
     88 	}
     89 	return null;
     90 }
     91 
     92 
     93 // EXPORTS //
     94 
     95 module.exports = validate;