validate.js (3013B)
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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ); 24 var isUnityProbabilityArray = require( '@stdlib/assert/is-unity-probability-array' ); 25 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 26 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; 27 var isObject = require( '@stdlib/assert/is-plain-object' ); 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 {NonNegativeInteger} [options.size] - sample size 39 * @param {ProbabilityArray} [options.probs] - element probabilities 40 * @param {boolean} [options.replace] - boolean indicating whether to sample with replacement 41 * @param {boolean} [options.mutate] - boolean indicating whether to mutate the `pool` when sampling without replacement 42 * @returns {(null|Error)} null or an error 43 * 44 * @example 45 * var opts = {}; 46 * var options = { 47 * 'size': 10, 48 * 'replace': false, 49 * 'mutate': true, 50 * 'probs': [ 0.7, 0.3 ] 51 * }; 52 * var err = validate( opts, options ); 53 * if ( err ) { 54 * throw err; 55 * } 56 */ 57 function validate( opts, options ) { 58 if ( !isObject( options ) ) { 59 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 60 } 61 if ( hasOwnProp( options, 'size' ) ) { 62 opts.size = options.size; 63 if ( !isNonNegativeInteger( opts.size ) ) { 64 return new TypeError( 'invalid option. `size` option must be a nonnegative integer. Option: `' + opts.size + '`.' ); 65 } 66 } 67 if ( hasOwnProp( options, 'probs' ) ) { 68 opts.probs = options.probs; 69 if ( !isUnityProbabilityArray( opts.probs ) ) { 70 return new TypeError( 'invalid option. `probs` option must be an array of probabilities that sum to one. Option: `' + opts.probs + '`.' ); 71 } 72 } 73 if ( hasOwnProp( options, 'mutate' ) ) { 74 opts.mutate = options.mutate; 75 if ( !isBoolean( opts.mutate ) ) { 76 return new TypeError( 'invalid option. `mutate` option must be a boolean primitive. Option: `' + opts.mutate + '`.' ); 77 } 78 } 79 if ( hasOwnProp( options, 'replace' ) ) { 80 opts.replace = options.replace; 81 if ( !isBoolean( opts.replace ) ) { 82 return new TypeError( 'invalid option. `replace` option must be a boolean primitive. Option: `' + opts.replace + '`.' ); 83 } 84 } 85 return null; 86 } 87 88 89 // EXPORTS // 90 91 module.exports = validate;