validate.js (4188B)
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 isString = require( '@stdlib/assert/is-string' ); 24 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 25 var isObject = require( '@stdlib/assert/is-plain-object' ); 26 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ); 27 var isPositiveNumberArray = require( '@stdlib/assert/is-positive-number-array' ); 28 var isFunction = require( '@stdlib/assert/is-function' ); 29 var isError = require( '@stdlib/assert/is-error' ); 30 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 31 var isnan = require( '@stdlib/assert/is-nan' ); 32 var getKernel = require( './get_kernel.js' ); 33 34 35 // MAIN // 36 37 /** 38 * Validates function options. 39 * 40 * @private 41 * @param {Object} opts - destination object 42 * @param {Options} options - function options 43 * @param {number} [options.n] - number of splits in the generated grid 44 * @param {NumericArray} [options.h] - array of length two indicating the x and y bandwidth values 45 * @param {number} [options.xMin] - lower limit of x 46 * @param {number} [options.xMax] - upper limit of x 47 * @param {number} [options.xMin] - lower limit of y 48 * @param {number} [options.yMax] - upper limit of y 49 * @param {(string|Function)} [options.kernel] - a string or function to specifying the used kernel function 50 * @returns {(Error|null)} null or an error object 51 * 52 * @example 53 * var opts = {}; 54 * var options = { 55 * 'xMin': 3.14, 56 * 'kernel': 'gaussian' 57 * }; 58 * var err = validate( opts, options ); 59 * if ( err ) { 60 * throw err; 61 * } 62 */ 63 function validate( opts, options ) { 64 if ( !isObject( options ) ) { 65 return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' ); 66 } 67 if ( hasOwnProp( options, 'h' ) ) { 68 opts.h = options.h; 69 if ( !isPositiveNumberArray( opts.h) ) { 70 return new TypeError( 'invalid option. `h` must be an array of positive values. Option: `' + opts.n + '`.'); 71 } 72 if ( opts.h.length !== 2 ) { 73 return new TypeError( 'invalid option. `h` must be an array of length two. Option: `' + opts.n + '`.'); 74 } 75 } 76 if ( hasOwnProp( options, 'n' ) ) { 77 opts.n = options.n; 78 if ( !isPositiveInteger( opts.n ) ) { 79 return new TypeError( 'invalid option. `n` option must be a positive integer. Option: `' + opts.n + '`.' ); 80 } 81 } 82 if ( hasOwnProp( options, 'xMax' ) ) { 83 opts.xMax = options.xMax; 84 if ( !isNumber( opts.xMax ) || isnan( opts.xMax ) ) { 85 return new TypeError( 'invalid option. `xMax` must be a number. Option: `' + opts.xMax + '`.' ); 86 } 87 } 88 if ( hasOwnProp( options, 'xMin' ) ) { 89 opts.xMin = options.xMin; 90 if ( !isNumber( opts.xMin ) || isnan( opts.xMin ) ) { 91 return new TypeError( 'invalid option. `xMin` must be a number. Option: `' + opts.xMin + '`.' ); 92 } 93 } 94 if ( hasOwnProp( options, 'yMax' ) ) { 95 opts.yMax = options.yMax; 96 if ( !isNumber( opts.yMax ) || isnan( opts.yMax ) ) { 97 return new TypeError( 'invalid option. `yMax` must be a number. Option: `' + opts.yMax + '`.' ); 98 } 99 } 100 if ( hasOwnProp( options, 'yMin' ) ) { 101 opts.yMin = options.yMin; 102 if ( !isNumber( opts.yMin ) || isnan( opts.yMin ) ) { 103 return new TypeError( 'invalid option. `yMin` must be a number. Option: `' + opts.yMin + '`.' ); 104 } 105 } 106 if ( hasOwnProp( options, 'kernel' ) ) { 107 opts.kernel = options.kernel; 108 if ( isString( opts.kernel ) ) { 109 opts.kernel = getKernel( opts.kernel ); 110 if ( isError( opts.kernel ) ) { 111 return opts.kernel; 112 } 113 } else if ( !isFunction( opts.kernel ) ) { 114 return new TypeError( 'Kernel is not a function from getKernel' ); 115 } 116 } 117 118 return null; 119 } 120 121 122 // EXPORTS // 123 124 module.exports = validate;