time-to-botec

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

validate.js (2132B)


      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 isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     25 var isObject = require( '@stdlib/assert/is-plain-object' );
     26 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Validates function options.
     33 *
     34 * @private
     35 * @param {Object} opts - destination for function options
     36 * @param {Options} options - function options
     37 * @param {boolean} [options.create] - boolean indicating whether to create a path if the key path does not already exist
     38 * @param {string} [options.sep] - key path separator
     39 * @returns {(Error|null)} error or null
     40 *
     41 * @example
     42 * var opts = {};
     43 * var options = {
     44 *     'sep': '/'
     45 * };
     46 * var err = validate( opts, options );
     47 * if ( err ) {
     48 *     throw err;
     49 * }
     50 */
     51 function validate( opts, options ) {
     52 	if ( !isObject( options ) ) {
     53 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     54 	}
     55 	if ( hasOwnProp( options, 'create' ) ) {
     56 		opts.create = options.create;
     57 		if ( !isBoolean( opts.create ) ) {
     58 			return new TypeError( 'invalid option. `create` option must be a boolean primitive. Option: `' + opts.create + '`.' );
     59 		}
     60 	}
     61 	if ( hasOwnProp( options, 'sep' ) ) {
     62 		opts.sep = options.sep;
     63 		if ( !isString( opts.sep ) ) {
     64 			return new TypeError( 'invalid option. `sep` option must be a string primitive. Option: `' + opts.sep + '`.' );
     65 		}
     66 	}
     67 	return null;
     68 }
     69 
     70 
     71 // EXPORTS //
     72 
     73 module.exports = validate;