time-to-botec

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

validate.js (2527B)


      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 isPlainObject = require( '@stdlib/assert/is-plain-object' );
     24 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     25 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     26 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     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 {string} [options.lpad] - string used to left pad
     39 * @param {string} [options.rpad] - string used to right pad
     40 * @param {boolean} [options.centerRight] - boolean indicating whether to center right in the event of a tie
     41 * @returns {(null|Error)} error object or null
     42 *
     43 * @example
     44 * var opts = {};
     45 * var options = {
     46 *     'lpad': 'a',
     47 *     'rpad': 'b'
     48 * };
     49 * var err = validate( opts, options );
     50 * if ( err ) {
     51 *     throw err;
     52 * }
     53 */
     54 function validate( opts, options ) {
     55 	if ( !isPlainObject( options ) ) {
     56 		return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
     57 	}
     58 	if ( hasOwnProp( options, 'lpad' ) ) {
     59 		opts.lpad = options.lpad;
     60 		if ( !isString( opts.lpad ) ) {
     61 			return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'lpad', opts.lpad ) );
     62 		}
     63 	}
     64 	if ( hasOwnProp( options, 'rpad' ) ) {
     65 		opts.rpad = options.rpad;
     66 		if ( !isString( opts.rpad ) ) {
     67 			return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'rpad', opts.rpad ) );
     68 		}
     69 	}
     70 	if ( hasOwnProp( options, 'centerRight' ) ) {
     71 		opts.centerRight = options.centerRight;
     72 		if ( !isBoolean( opts.centerRight ) ) {
     73 			return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'centerRight', opts.centerRight ) );
     74 		}
     75 	}
     76 	return null;
     77 }
     78 
     79 
     80 // EXPORTS //
     81 
     82 module.exports = validate;