validate.js (3089B)
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 isPositiveNumber = require( '@stdlib/assert/is-positive-number' ); 26 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 27 var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive; 28 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; 29 30 31 // MAIN // 32 33 /** 34 * Validates function options. 35 * 36 * @private 37 * @param {Object} opts - destination object 38 * @param {Options} options - function options 39 * @param {Probability} [options.f] - smoother span (proportion of points which influence smoothing at each value) 40 * @param {NonNegativeInteger} [options.nsteps] - number of iterations in the robust fit 41 * @param {NonNegativeNumber} [options.delta] - nonnegative parameter which may be used to save computations 42 * @param {boolean} [options.sorted] - boolean indicating if the input array is in sorted order 43 * @returns {(Error|null)} null or an error object 44 * 45 * @example 46 * var opts = {}; 47 * var options = { 48 * 'f': 0.75, 49 * 'nsteps': 6, 50 * 'delta': 0.03, 51 * 'sorted': true 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 must be an object. Value: `' + options + '`.' ); 61 } 62 if ( hasOwnProp( options, 'f' ) ) { 63 opts.f = options.f; 64 if ( !isPositiveNumber( opts.f ) ) { 65 return new TypeError( 'invalid option. `f` option must be a positive number. Option: `' + opts.f + '`.' ); 66 } 67 } 68 if ( hasOwnProp( options, 'nsteps' ) ) { 69 opts.nsteps = options.nsteps; 70 if ( !isNonNegativeInteger( opts.nsteps ) ) { 71 return new TypeError( 'invalid option. `nsteps` option must be a nonnegative integer. Option: `' + opts.nsteps + '`.' ); 72 } 73 } 74 if ( hasOwnProp( options, 'delta' ) ) { 75 opts.delta = options.delta; 76 if ( !isNonNegativeNumber( opts.delta ) ) { 77 return new TypeError( 'invalid option. `delta` option must be a nonnegative number. Option: `' + opts.delta + '`.' ); 78 } 79 } 80 if ( hasOwnProp( options, 'sorted' ) ) { 81 opts.sorted = options.sorted; 82 if ( !isBoolean( opts.sorted ) ) { 83 return new TypeError( 'invalid option. `sorted` option must be a boolean primitive. Option: `' + opts.sorted + '`.' ); 84 } 85 } 86 return null; 87 } 88 89 90 // EXPORTS // 91 92 module.exports = validate;