time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

validate.js (2052B)


      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 isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
     24 var isObject = require( '@stdlib/assert/is-plain-object' );
     25 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Validates function options.
     32 *
     33 * @private
     34 * @param {Object} opts - destination object
     35 * @param {Options} options - function options
     36 * @param {PositiveNumber} [options.alpha] - Dirichlet hyper-parameter of topic vector theta:
     37 * @param {PositiveNumber} [options.beta] - Dirichlet hyper-parameter for word vector phi
     38 * @returns {(Error|null)} null or an error object
     39 *
     40 *
     41 * @example
     42 * var opts = {};
     43 * var options = {};
     44 * var err = validate( opts, options );
     45 * if ( err ) {
     46 *     throw err;
     47 * }
     48 */
     49 function validate( opts, options ) {
     50 	if ( !isObject( options ) ) {
     51 		return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
     52 	}
     53 	if ( hasOwnProp( options, 'alpha' ) ) {
     54 		opts.alpha = options.alpha;
     55 		if ( !isPositive( opts.alpha ) ) {
     56 			return new TypeError( 'invalid option. `alpha` option must be a positive number. Option: `' + opts.alpha + '`.' );
     57 		}
     58 	}
     59 	if ( hasOwnProp( options, 'beta' ) ) {
     60 		opts.beta = options.beta;
     61 		if ( !isPositive( opts.beta ) ) {
     62 			return new TypeError( 'invalid option. `beta` option must be a positive number. Option: `' + opts.beta + '`.' );
     63 		}
     64 	}
     65 	return null;
     66 }
     67 
     68 
     69 // EXPORTS //
     70 
     71 module.exports = validate;