time-to-botec

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

validate_table.js (2411B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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 isFunction = require( '@stdlib/assert/is-function' );
     26 var isNull = require( '@stdlib/assert/is-null' );
     27 var objectKeys = require( '@stdlib/utils/keys' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Validates a resolution table object.
     34 *
     35 * @private
     36 * @param {Object} out - destination object
     37 * @param {Object} table - resolution table object
     38 * @param {(Function|null)} [table.number] - function to invoke upon receiving a number
     39 * @param {(Function|null)} [table.complex] - function to invoke upon receiving a complex number
     40 * @param {(Function|null)} [table.array] - function to invoke upon receiving an array-like object
     41 * @param {(Function|null)} [table.ndarray] - function to invoke upon receiving an ndarray-like object
     42 * @returns {(Error|null)} null or an error object
     43 *
     44 * @example
     45 * var out = {};
     46 * var table = {
     47 *     'number': null,
     48 *     'complex': null,
     49 *     'array': null,
     50 *     'ndarray': null
     51 * };
     52 * var err = validate( out, table );
     53 * if ( err ) {
     54 *     throw err;
     55 * }
     56 */
     57 function validate( out, table ) {
     58 	var fields;
     59 	var tmp;
     60 	var key;
     61 	var i;
     62 
     63 	if ( !isPlainObject( table ) ) {
     64 		return new TypeError( 'invalid argument. Resolution table must be a plain object. Value: `' + table + '`.' );
     65 	}
     66 	fields = objectKeys( out );
     67 	for ( i = 0; i < fields.length; i++ ) {
     68 		key = fields[ i ];
     69 		if ( hasOwnProp( table, key ) ) {
     70 			tmp = table[ key ];
     71 			if ( !isFunction( tmp ) && !isNull( tmp ) ) {
     72 				return new TypeError( 'invalid argument. Resolution table `' + key + '` field value must be either a function or `null`. Value: `' + tmp + '`.' );
     73 			}
     74 			out[ key ] = tmp;
     75 		}
     76 	}
     77 	return null;
     78 }
     79 
     80 
     81 // EXPORTS //
     82 
     83 module.exports = validate;