time-to-botec

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

validate.js (2778B)


      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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     26 var isFunction = require( '@stdlib/assert/is-function' );
     27 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     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 {number} [options.level] - merge level
     39 * @param {boolean} [options.copy] - boolean indicating whether to deep copy merged values
     40 * @param {(boolean|Function)} [options.override] - defines the merge strategy
     41 * @param {boolean} [options.extend] - boolean indicating whether new properties can be added to the target object
     42 * @returns {(Error|null)} error object or null
     43 */
     44 function validate( opts, options ) {
     45 	if ( !isObject( options ) ) {
     46 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     47 	}
     48 	if ( hasOwnProp( options, 'level' ) ) {
     49 		opts.level = options.level;
     50 		if ( !isPositiveInteger( opts.level ) ) {
     51 			return new TypeError( 'invalid option. `level` option must be a positive integer. Option: `' + opts.level + '`.' );
     52 		}
     53 	}
     54 	if ( hasOwnProp( options, 'copy' ) ) {
     55 		opts.copy = options.copy;
     56 		if ( !isBoolean( opts.copy ) ) {
     57 			return new TypeError( 'invalid option. `copy` option must be a boolean primitive. Option: `' + opts.copy + '`.' );
     58 		}
     59 	}
     60 	if ( hasOwnProp( options, 'override' ) ) {
     61 		opts.override = options.override;
     62 		if (
     63 			!isBoolean( opts.override ) &&
     64 			!isFunction( opts.override )
     65 		) {
     66 			return new TypeError( 'invalid option. `override` option must be either a boolean primitive or a function. Option: `' + opts.override + '`.' );
     67 		}
     68 	}
     69 	if ( hasOwnProp( options, 'extend' ) ) {
     70 		opts.extend = options.extend;
     71 		if ( !isBoolean( opts.extend ) ) {
     72 			return new TypeError( 'invalid option. `extend` option must be a boolean primitive. Option: `' + opts.extend + '`.' );
     73 		}
     74 	}
     75 	return null;
     76 }
     77 
     78 
     79 // EXPORTS //
     80 
     81 module.exports = validate;