time-to-botec

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

validate.js (4457B)


      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 contains = require( '@stdlib/assert/contains' );
     24 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     25 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     26 var isObject = require( '@stdlib/assert/is-plain-object' );
     27 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     28 var isnan = require( '@stdlib/assert/is-nan' );
     29 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     30 
     31 
     32 // VARIABLES //
     33 
     34 var ALTERNATIVE_VALUES = [ 'two-sided', 'less', 'greater' ];
     35 var ZERO_METHOD_VALUES = [ 'pratt', 'wilcox', 'zsplit' ];
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Validates function options.
     42 *
     43 * @private
     44 * @param {Object} opts - destination for validated options
     45 * @param {Options} options - function options
     46 * @param {number} [options.alpha] - significance level
     47 * @param {string} [options.alternative] - alternative hypothesis (`two-sided`, `less` or `greater`)
     48 * @param {boolean} [options.exact] - whether to force using the exact distribution instead of a normal approximation when there are more than fifty data points
     49 * @param {boolean} [options.correction] - continuity correction adjusting the Wilcoxon rank statistic by 0.5 towards the mean
     50 * @param {string} [options.zeroMethod] - method governing how zero-differences are handled (`pratt`, `wilcox` or `zsplit`)
     51 * @param {number} [options.mu] - mean under H0
     52 * @returns {(null|Error)} null or an error
     53 */
     54 function validate( opts, options ) {
     55 	if ( !isObject( options ) ) {
     56 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     57 	}
     58 	if ( hasOwnProp( options, 'alpha' ) ) {
     59 		opts.alpha = options.alpha;
     60 		if ( !isNumber( opts.alpha ) || isnan( opts.alpha ) ) {
     61 			return new TypeError( 'invalid option. `alpha` option must be a number primitive. Option: `' + opts.alpha + '`.' );
     62 		}
     63 		if ( opts.alpha < 0.0 || opts.alpha > 1.0 ) {
     64 			return new RangeError( 'invalid argument. Option `alpha` must be a number in the range 0 to 1. Value: `' + opts.alpha + '`.' );
     65 		}
     66 	}
     67 	if ( hasOwnProp( options, 'alternative' ) ) {
     68 		opts.alternative = options.alternative;
     69 		if ( !isString( opts.alternative ) ) {
     70 			return new TypeError( 'invalid option. `alternative` option must be a string primitive. Option: `' + opts.alternative + '`.' );
     71 		}
     72 		if ( !contains( ALTERNATIVE_VALUES, opts.alternative ) ) {
     73 			return new Error( 'invalid option. `alternative` option must be one of '+ALTERNATIVE_VALUES.join( ', ' )+'. Option: `' + opts.alternative + '`.' );
     74 		}
     75 	}
     76 	if ( hasOwnProp( options, 'correction' ) ) {
     77 		opts.correction = options.correction;
     78 		if ( !isBoolean( opts.correction ) || isnan( opts.correction ) ) {
     79 			return new TypeError( 'invalid option. `correction` option must be a boolean primitive. Option: `' + opts.alpha + '`.' );
     80 		}
     81 	}
     82 	if ( hasOwnProp( options, 'exact' ) ) {
     83 		opts.exact = options.exact;
     84 		if (
     85 			!isBoolean( opts.exact ) ||
     86 			isnan( opts.exact )
     87 		) {
     88 			return new TypeError( 'invalid option. `exact` option must be a boolean primitive. Option: `' + opts.alpha + '`.' );
     89 		}
     90 	}
     91 	if ( hasOwnProp( options, 'mu' ) ) {
     92 		opts.mu = options.mu;
     93 		if ( !isNumber( opts.mu ) || isnan( opts.mu ) ) {
     94 			return new TypeError( 'invalid option. `mu` option must be a number primitive. Option: `' + opts.mu + '`.' );
     95 		}
     96 	}
     97 	if ( hasOwnProp( options, 'zeroMethod' ) ) {
     98 		opts.zeroMethod = options.zeroMethod;
     99 		if ( !isString( opts.zeroMethod ) ) {
    100 			return new TypeError( 'invalid option. `zeroMethod` option must be a string primitive. Option: `' + opts.alternative + '`.' );
    101 		}
    102 		if ( !contains( ZERO_METHOD_VALUES, opts.zeroMethod ) ) {
    103 			return new Error( 'invalid option. `zeroMethod` option must be one of '+ZERO_METHOD_VALUES.join( ', ' )+'. Option: `' + opts.zeroMethod + '`.' );
    104 		}
    105 	}
    106 	return null;
    107 }
    108 
    109 
    110 // EXPORTS //
    111 
    112 module.exports = validate;