validate.js (2861B)
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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); 24 var isObject = require( '@stdlib/assert/is-plain-object' ); 25 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 26 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 27 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 28 var isnan = require( '@stdlib/assert/is-nan' ); 29 30 31 // MAIN // 32 33 /** 34 * Validates function options. 35 * 36 * @private 37 * @param {Object} opts - destination for validated options 38 * @param {Options} options - function options 39 * @param {number} [options.alpha] - significance level 40 * @param {string} [options.alternative] - alternative hypothesis 41 * @param {NonNegativeInteger} [options.init] - number of data points used to compute initial statistics 42 * @returns {(null|Error)} null or an error 43 * 44 * @example 45 * var opts = {}; 46 * 47 * var options = { 48 * 'alpha': 0.05, 49 * 'alernative': 'two-sided', 50 * 'init': 100 51 * }; 52 * 53 * var err = validate( opts, options ); 54 * if ( err ) { 55 * throw err; 56 * } 57 */ 58 function validate( opts, options ) { 59 if ( !isObject( options ) ) { 60 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 61 } 62 if ( hasOwnProp( options, 'alpha' ) ) { 63 opts.alpha = options.alpha; 64 if ( !isNumber( opts.alpha ) || isnan( opts.alpha ) ) { 65 return new TypeError( 'invalid option. `alpha` option must be a number primitive. Option: `' + opts.alpha + '`.' ); 66 } 67 if ( opts.alpha < 0.0 || opts.alpha > 1.0 ) { 68 return new RangeError( 'invalid option. `alpha` option must be on the interval [0,1]. Option: `' + opts.alpha + '`.' ); 69 } 70 } 71 if ( hasOwnProp( options, 'alternative' ) ) { 72 opts.alternative = options.alternative; 73 if ( !isString( opts.alternative ) ) { 74 return new TypeError( 'invalid option. `alternative` option must be a string primitive. Option: `' + opts.alternative + '`.' ); 75 } 76 } 77 if ( hasOwnProp( options, 'init' ) ) { 78 opts.init = options.init; 79 if ( !isNonNegativeInteger( opts.init ) ) { 80 return new TypeError( 'invalid option. `init` option must be a nonnegative integer. Option: `' + opts.init + '`.' ); 81 } 82 } 83 return null; 84 } 85 86 87 // EXPORTS // 88 89 module.exports = validate;