validate.js (2464B)
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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; 24 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 25 var isObject = require( '@stdlib/assert/is-plain-object' ); 26 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 27 var isnan = require( '@stdlib/assert/is-nan' ); 28 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 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 {boolean} [options.sorted] - boolean indicating if the input data is already sorted in ascending order 41 * @param {string} [options.alternative] - alternative hypothesis 42 * @returns {(null|Error)} null or an error 43 */ 44 function validate( opts, options ) { 45 if ( !isObject( options ) ) { 46 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 47 } 48 if ( hasOwnProp( options, 'alpha' ) ) { 49 opts.alpha = options.alpha; 50 if ( !isNumber( opts.alpha ) || isnan( opts.alpha ) ) { 51 return new TypeError( 'invalid option. `alpha` option must be a number primitive. Option: `' + opts.alpha + '`.' ); 52 } 53 } 54 if ( hasOwnProp( options, 'alternative' ) ) { 55 opts.alternative = options.alternative; 56 if ( !isString( opts.alternative ) ) { 57 return new TypeError( 'invalid option. `alternative` option must be a string primitive. Option: `' + opts.alternative + '`.' ); 58 } 59 } 60 if ( hasOwnProp( options, 'sorted' ) ) { 61 opts.sorted = options.sorted; 62 if ( !isBoolean( opts.sorted ) ) { 63 return new TypeError( 'invalid option. `sorted` flag must be a boolean primitive. Option: `' + opts.sorted + '`.' ); 64 } 65 } 66 return null; 67 } 68 69 70 // EXPORTS // 71 72 module.exports = validate;