time-to-botec

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

validate.js (1957B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
     24 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     25 var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
     26 var isEmptyArray = require( '@stdlib/assert/is-empty-array' );
     27 var format = require( './../../format' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Validates function options.
     34 *
     35 * @private
     36 * @param {Object} opts - destination object
     37 * @param {Options} options - options to validate
     38 * @param {StringArray} [options.stopwords] - array of custom stop words
     39 * @returns {(null|Error)} error object or null
     40 *
     41 * @example
     42 * var opts = {};
     43 * var options = {
     44 *     'stopwords': [ 'of' ]
     45 * };
     46 * var err = validate( opts, options );
     47 * if ( err ) {
     48 *     throw err;
     49 * }
     50 */
     51 function validate( opts, options ) {
     52 	if ( !isPlainObject( options ) ) {
     53 		return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
     54 	}
     55 	if ( hasOwnProp( options, 'stopwords' ) ) {
     56 		opts.stopwords = options.stopwords;
     57 		if (
     58 			!isStringArray( opts.stopwords ) &&
     59 			!isEmptyArray( opts.stopwords )
     60 		) {
     61 			return new TypeError( format( 'invalid option. `%s` option must be an array of strings. Option: `%s`.', 'stopwords', opts.stopwords ) );
     62 		}
     63 	}
     64 	return null;
     65 }
     66 
     67 
     68 // EXPORTS //
     69 
     70 module.exports = validate;