time-to-botec

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

validate.js (2681B)


      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 isObject = require( '@stdlib/assert/is-plain-object' );
     24 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     25 var isArray = require( '@stdlib/assert/is-array' );
     26 var isIndexMode = require( './../../base/assert/is-index-mode' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Validates function options.
     33 *
     34 * @private
     35 * @param {Object} opts - destination object
     36 * @param {Options} options - function options
     37 * @param {string} [options.mode] - specifies how to handle indices which exceed array dimensions
     38 * @param {string} [options.submode] - specifies how to handle subscripts which exceed array dimensions
     39 * @returns {(Error|null)} null or an error object
     40 *
     41 * @example
     42 * var opts = {};
     43 * var options = {
     44 *     'mode': 'clamp',
     45 *     'submode': [ 'throw', 'wrap', 'clamp' ]
     46 * };
     47 * var err = validate( opts, options );
     48 * if ( err ) {
     49 *     throw err;
     50 * }
     51 */
     52 function validate( opts, options ) {
     53 	var i;
     54 	if ( !isObject( options ) ) {
     55 		return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
     56 	}
     57 	if ( hasOwnProp( options, 'mode' ) ) {
     58 		opts.mode = options.mode;
     59 		if ( !isIndexMode( opts.mode ) ) {
     60 			return new TypeError( 'invalid option. `mode` option must be a recognized mode. Option: `' + opts.mode + '`.' );
     61 		}
     62 	}
     63 	if ( hasOwnProp( options, 'submode' ) ) {
     64 		opts.submode = options.submode;
     65 		if ( !isArray( opts.submode ) ) {
     66 			return new TypeError( 'invalid option. `submode` option must be an array containing recognized modes. Option: `' + opts.submode + '`.' );
     67 		}
     68 		if ( opts.submode.length === 0 ) {
     69 			return new TypeError( 'invalid option. `submode` option must be an array containing recognized modes. Option: `' + opts.submode.join( ',' ) + '`.' );
     70 		}
     71 		for ( i = 0; i < opts.submode.length; i++ ) {
     72 			if ( !isIndexMode( opts.submode[ i ] ) ) {
     73 				return new TypeError( 'invalid option. Each `submode` must be a recognized modes. Option: `' + opts.submode[ i ] + '`.' );
     74 			}
     75 		}
     76 		opts.submode = opts.submode.slice();
     77 	}
     78 	return null;
     79 }
     80 
     81 
     82 // EXPORTS //
     83 
     84 module.exports = validate;