time-to-botec

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

validate.js (2458B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     24 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     25 var isObject = require( '@stdlib/assert/is-plain-object' );
     26 var isnan = require( '@stdlib/assert/is-nan' );
     27 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Validates function options.
     34 *
     35 * @private
     36 * @param {Object} opts - destination for validated options
     37 * @param {Options} options - function options
     38 * @param {number} [options.alpha] - significance level
     39 * @param {boolean} [options.correct] - boolean indicating whether to use Yates' continuity correction when provided a 2x2 contingency table
     40 * @returns {(null|Error)} null or an error
     41 *
     42 * @example
     43 * var opts = {};
     44 * var options = {
     45 *     'alpha': 0.01,
     46 *     'correct': true
     47 * };
     48 * var err = validate( opts, options );
     49 * if ( err ) {
     50 *     throw err;
     51 * }
     52 */
     53 function validate( opts, options ) {
     54 	if ( !isObject( options ) ) {
     55 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     56 	}
     57 	if ( hasOwnProp( options, 'alpha' ) ) {
     58 		opts.alpha = options.alpha;
     59 		if ( !isNumber( opts.alpha ) || isnan( opts.alpha ) ) {
     60 			return new TypeError( 'invalid option. `alpha` option must be a number primitive. Option: `' + opts.alpha + '`.' );
     61 		}
     62 		if ( opts.alpha < 0.0 || opts.alpha > 1.0 ) {
     63 			return new RangeError( 'invalid option. `alpha` option must be a number on the interval `[0,1]`. Value: `' + opts.alpha + '`.' );
     64 		}
     65 	}
     66 	if ( hasOwnProp( options, 'correct' ) ) {
     67 		opts.correct = options.correct;
     68 		if ( !isBoolean( opts.correct ) ) {
     69 			return new TypeError( 'invalid option. `correct` option must be a boolean primitive. Option: `' + opts.simulate + '`.' );
     70 		}
     71 	}
     72 	return null;
     73 }
     74 
     75 
     76 // EXPORTS //
     77 
     78 module.exports = validate;