validate.js (1841B)
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' ).isPrimitive; 24 25 26 // MAIN // 27 28 /** 29 * Validates parameters. 30 * 31 * @private 32 * @param {NonNegativeInteger} N - population size 33 * @param {NonNegativeInteger} K - subpopulation size 34 * @param {NonNegativeInteger} n - number of draws 35 * @returns {(Error|null)} error or null 36 * 37 * @example 38 * var err = validate( 1, 2, 3 ); 39 * if ( err ) { 40 * throw err; 41 * } 42 */ 43 function validate( N, K, n ) { 44 if ( !isNonNegativeInteger( N ) ) { 45 return new TypeError( 'invalid argument. `N` must be a nonnegative integer. Value: `' + N + '`.' ); 46 } 47 if ( !isNonNegativeInteger( K ) ) { 48 return new TypeError( 'invalid argument. `K` must be a nonnegative integer. Value: `' + K + '`.' ); 49 } 50 if ( !isNonNegativeInteger( n ) ) { 51 return new TypeError( 'invalid argument. `n` must be a nonnegative integer. Value: `' + n + '`.' ); 52 } 53 if ( n > N ) { 54 return new RangeError( 'invalid argument. `n` must be less than or equal to `N`. Value: `' + n + '`.' ); 55 } 56 if ( K > N ) { 57 return new RangeError( 'invalid argument. `K` must be less than or equal to `N`. Value: `' + K + '`.' ); 58 } 59 return null; 60 } 61 62 63 // EXPORTS // 64 65 module.exports = validate;