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