validate.js (2362B)
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/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 {string} [options.alternative] - alternative hypothesis (`two-sided`, `less` or `greater`) 40 * @param {number} [options.mu] - mean under H0 41 * @returns {(null|Error)} null or an error 42 */ 43 function validate( opts, options ) { 44 if ( !isObject( options ) ) { 45 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 46 } 47 if ( hasOwnProp( options, 'alpha' ) ) { 48 opts.alpha = options.alpha; 49 if ( !isNumber( opts.alpha ) || isnan( opts.alpha ) ) { 50 return new TypeError( 'invalid option. `alpha` option must be a number primitive. Option: `' + opts.alpha + '`.' ); 51 } 52 } 53 if ( hasOwnProp( options, 'alternative' ) ) { 54 opts.alternative = options.alternative; 55 if ( !isString( opts.alternative ) ) { 56 return new TypeError( 'invalid option. `alternative` option must be a string primitive. Option: `' + opts.alternative + '`.' ); 57 } 58 } 59 if ( hasOwnProp( options, 'mu' ) ) { 60 opts.mu = options.mu; 61 if ( !isNumber( opts.mu ) || isnan( opts.mu ) ) { 62 return new TypeError( 'invalid option. `mu` option must be a number primitive. Option: `' + opts.mu + '`.' ); 63 } 64 } 65 return null; 66 } 67 68 69 // EXPORTS // 70 71 module.exports = validate;