time-to-botec

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

validate.js (4865B)


      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 isNonNegative = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive;
     27 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     28 var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
     29 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Validates function options.
     36 *
     37 * @private
     38 * @param {Object} opts - destination object
     39 * @param {Options} options - function options
     40 * @param {string} [options.sep] - separator used to join streamed data
     41 * @param {boolean} [options.objectMode] - specifies whether a stream should operate in object mode
     42 * @param {(string|null)} [options.encoding] - specifies how `Buffer` objects should be decoded to `strings`
     43 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in the internal buffer before ceasing to generate additional pseudorandom numbers
     44 * @param {NonNegativeInteger} [options.iter] - number of iterations
     45 * @param {string} [options.name] - name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers
     46 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
     47 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
     48 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
     49 * @param {boolean} [options.copy] - boolean indicating whether to copy a provided pseudorandom number generator state
     50 * @param {PositiveInteger} [options.siter] - number of iterations after which to emit the PRNG state
     51 * @returns {(Error|null)} null or an error object
     52 *
     53 * @example
     54 * var opts = {};
     55 * var options = {
     56 *     'objectMode': true
     57 * };
     58 * var err= validate( opts, options );
     59 * if ( err ) {
     60 *     throw err;
     61 * }
     62 */
     63 function validate( opts, options ) {
     64 	if ( !isObject( options ) ) {
     65 		return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' );
     66 	}
     67 	if ( hasOwnProp( options, 'sep' ) ) {
     68 		opts.sep = options.sep;
     69 		if ( !isString( opts.sep ) ) {
     70 			return new TypeError( 'invalid option. `sep` option must be a primitive string. Option: `' + opts.sep + '`.' );
     71 		}
     72 	}
     73 	if ( hasOwnProp( options, 'objectMode' ) ) {
     74 		opts.objectMode = options.objectMode;
     75 		if ( !isBoolean( opts.objectMode ) ) {
     76 			return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' );
     77 		}
     78 	}
     79 	if ( hasOwnProp( options, 'encoding' ) ) {
     80 		opts.encoding = options.encoding;
     81 		if ( !isString( opts.encoding ) && opts.encoding !== null ) {
     82 			return new TypeError( 'invalid option. `encoding` option must be a primitive string or null. Option: `' + opts.encoding + '`.' );
     83 		}
     84 	}
     85 	if ( hasOwnProp( options, 'highWaterMark' ) ) {
     86 		opts.highWaterMark = options.highWaterMark;
     87 		if ( !isNonNegative( opts.highWaterMark ) ) {
     88 			return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' );
     89 		}
     90 	}
     91 	if ( hasOwnProp( options, 'iter' ) ) {
     92 		opts.iter = options.iter;
     93 		if ( !isNonNegativeInteger( opts.iter ) ) {
     94 			return new TypeError( 'invalid option. `iter` option must be a nonnegative integer. Option: `' + opts.iter + '`.' );
     95 		}
     96 	}
     97 	if ( hasOwnProp( options, 'siter' ) ) {
     98 		opts.siter = options.siter;
     99 		if ( !isPositiveInteger( opts.siter ) ) {
    100 			return new TypeError( 'invalid option. `siter` option must be a positive integer. Option: `' + opts.siter + '`.' );
    101 		}
    102 	}
    103 	// Pass through options...
    104 	if ( hasOwnProp( options, 'name' ) ) {
    105 		opts.name = options.name;
    106 	}
    107 	if ( hasOwnProp( options, 'prng' ) ) {
    108 		opts.prng = options.prng;
    109 	}
    110 	if ( hasOwnProp( options, 'seed' ) ) {
    111 		opts.seed = options.seed;
    112 	}
    113 	if ( hasOwnProp( options, 'state' ) ) {
    114 		opts.state = options.state;
    115 	}
    116 	if ( hasOwnProp( options, 'copy' ) ) {
    117 		opts.copy = options.copy;
    118 	}
    119 	return null;
    120 }
    121 
    122 
    123 // EXPORTS //
    124 
    125 module.exports = validate;