time-to-botec

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

validate.js (2968B)


      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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     26 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
     27 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Validates function options.
     34 *
     35 * @private
     36 * @param {Object} opts - destination for function options
     37 * @param {Options} options - function options
     38 * @param {NonNegativeInteger} [options.depth] - depth to flatten
     39 * @param {boolean} [options.copy] - boolean indicating whether to deep copy
     40 * @param {boolean} [options.flattenArrays] - boolean indicating whether to flatten arrays
     41 * @param {string} [options.delimiter] - key path delimiter
     42 * @returns {(Error|null)} error or null
     43 *
     44 * @example
     45 * var opts = {};
     46 * var options = {
     47 *     'depth': 10,
     48 *     'copy': false,
     49 *     'delimiter': '|',
     50 *     'flattenArrays': false
     51 * };
     52 * var err = validate( opts, options );
     53 * if ( err ) {
     54 *     throw err;
     55 * }
     56 */
     57 function validate( opts, options ) {
     58 	if ( !isObject( options ) ) {
     59 		return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     60 	}
     61 	if ( hasOwnProp( options, 'depth' ) ) {
     62 		opts.depth = options.depth;
     63 		if ( !isNonNegativeInteger( opts.depth ) ) {
     64 			return new TypeError( 'invalid option. `depth` option must be a nonnegative integer. Option: `' + opts.depth + '`.' );
     65 		}
     66 	}
     67 	if ( hasOwnProp( options, 'copy' ) ) {
     68 		opts.copy = options.copy;
     69 		if ( !isBoolean( opts.copy ) ) {
     70 			return new TypeError( 'invalid option. `copy` option must be a boolean primitive. Option: `' + opts.copy + '`.' );
     71 		}
     72 	}
     73 	if ( hasOwnProp( options, 'flattenArrays' ) ) {
     74 		opts.flattenArrays = options.flattenArrays;
     75 		if ( !isBoolean( opts.flattenArrays ) ) {
     76 			return new TypeError( 'invalid option. `flattenArrays` option must be a boolean primitive. Option: `' + opts.flattenArrays + '`.' );
     77 		}
     78 	}
     79 	if ( hasOwnProp( options, 'delimiter' ) ) {
     80 		opts.delimiter = options.delimiter;
     81 		if ( !isString( opts.delimiter ) ) {
     82 			return new TypeError( 'invalid option. `delimiter` option must be a string primitive. Option: `' + opts.delimiter + '`.' );
     83 		}
     84 	}
     85 	return null;
     86 }
     87 
     88 
     89 // EXPORTS //
     90 
     91 module.exports = validate;