time-to-botec

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

validate.js (2777B)


      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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     24 var isObject = require( '@stdlib/assert/is-plain-object' );
     25 var isnan = require( '@stdlib/assert/is-nan' );
     26 var indexOf = require( '@stdlib/utils/index-of' );
     27 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     28 
     29 
     30 // VARIABLES //
     31 
     32 var alternative = [ 'two-sided', 'less', 'greater' ];
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Validates function options.
     39 *
     40 * @private
     41 * @param {Object} opts - destination for validated options
     42 * @param {Options} options - function options
     43 * @param {number} [options.alpha] - significance level
     44 * @param {string} [options.alternative] - alternative hypothesis (`two-sided`, `less` or `greater`)
     45 * @param {number} [options.rho] - correlation coefficient unter HO
     46 * @returns {(null|Error)} null or an error
     47 *
     48 * @example
     49 * var opts = {};
     50 * var options = {
     51 *     'alpha': 0.01,
     52 *     'rho': 0.8
     53 * };
     54 * var err = validate( opts, options );
     55 * if ( err ) {
     56 *     throw err;
     57 * }
     58 */
     59 function validate( opts, options ) {
     60 	if ( !isObject( options ) ) {
     61 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     62 	}
     63 	if ( hasOwnProp( options, 'alpha' ) ) {
     64 		opts.alpha = options.alpha;
     65 		if (
     66 			!isNumber( opts.alpha ) ||
     67 			isnan( opts.alpha ) ||
     68 			opts.alpha < 0.0 ||
     69 			opts.alpha > 1.0
     70 		) {
     71 			return new TypeError( 'invalid option. `alpha` option must be a number in `[0,1]`. Option: `' + opts.alpha + '`.' );
     72 		}
     73 	}
     74 	if ( hasOwnProp( options, 'alternative' ) ) {
     75 		opts.alternative = options.alternative;
     76 		if ( indexOf( alternative, opts.alternative ) === -1 ) {
     77 			return new TypeError( 'invalid option. `alternative` option must be one of the following: "' + alternative.join( '", "' ) + '". Option: `' + opts.alternative + '`.' );
     78 		}
     79 	}
     80 	if ( hasOwnProp( options, 'rho' ) ) {
     81 		opts.rho = options.rho;
     82 		if (
     83 			!isNumber( opts.rho ) ||
     84 			isnan( opts.rho ) ||
     85 			opts.rho < -1.0 ||
     86 			opts.rho > 1.0
     87 		) {
     88 			return new TypeError( 'invalid option. `rho` option must be a number in [-1,1]. Option: `' + opts.rho + '`.' );
     89 		}
     90 	}
     91 	return null;
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = validate;