validate.js (2212B)
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 isOrder = require( './../../base/assert/is-order' ); 26 var isIndexMode = require( './../../base/assert/is-index-mode' ); 27 28 29 // MAIN // 30 31 /** 32 * Validates function options. 33 * 34 * @private 35 * @param {Object} opts - destination object 36 * @param {Options} options - function options 37 * @param {string} [options.mode] - specifies how to handle a linear index which exceeds array dimensions 38 * @param {string} [options.order] - specifies whether an array is row-major (C-style) or column-major (Fortran-style) 39 * @returns {(Error|null)} null or an error object 40 * 41 * @example 42 * var opts = {}; 43 * var options = { 44 * 'mode': 'throw', 45 * 'order': 'column-major' 46 * }; 47 * var err = validate( opts, options ); 48 * if ( err ) { 49 * throw err; 50 * } 51 */ 52 function validate( opts, options ) { 53 if ( !isObject( options ) ) { 54 return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' ); 55 } 56 if ( hasOwnProp( options, 'mode' ) ) { 57 opts.mode = options.mode; 58 if ( !isIndexMode( opts.mode ) ) { 59 return new TypeError( 'invalid option. `mode` option must be a supported/recognized mode. Option: `' + opts.mode + '`.' ); 60 } 61 } 62 if ( hasOwnProp( options, 'order' ) ) { 63 opts.order = options.order; 64 if ( !isOrder( opts.order ) ) { 65 return new TypeError( 'invalid option. `order` option must be a supported/recognized order. Option: `' + opts.order + '`.' ); 66 } 67 } 68 return null; 69 } 70 71 72 // EXPORTS // 73 74 module.exports = validate;