validate.js (3307B)
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 29 30 // MAIN // 31 32 /** 33 * Validates function options. 34 * 35 * @private 36 * @param {Object} opts - destination object 37 * @param {Options} options - function options 38 * @param {string} [options.name] - debug namespace 39 * @param {boolean} [options.objectMode] - specifies whether a stream should operate in object mode 40 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false` 41 * @param {boolean} [options.allowHalfOpen] - specifies whether the stream should remain open even if one side ends 42 * @param {boolean} [options.readableObjectMode] - specifies whether the readable side should be in object mode 43 * @returns {(Error|null)} null or an error object 44 */ 45 function validate( opts, options ) { 46 if ( !isObject( options ) ) { 47 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 48 } 49 if ( hasOwnProp( options, 'name' ) ) { 50 opts.name = options.name; 51 if ( !isString( opts.name ) ) { 52 return new TypeError( 'invalid option. `name` option must be a primitive string. Option: `' + opts.name + '`.' ); 53 } 54 } 55 if ( hasOwnProp( options, 'objectMode' ) ) { 56 opts.objectMode = options.objectMode; 57 if ( !isBoolean( opts.objectMode ) ) { 58 return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' ); 59 } 60 } 61 if ( hasOwnProp( options, 'readableObjectMode' ) ) { 62 opts.readableObjectMode = options.readableObjectMode; 63 if ( !isBoolean( opts.readableObjectMode ) ) { 64 return new TypeError( 'invalid option. `readableObjectMode` option must be a primitive boolean. Option: `' + opts.readableObjectMode + '`.' ); 65 } 66 } 67 if ( hasOwnProp( options, 'allowHalfOpen' ) ) { 68 opts.allowHalfOpen = options.allowHalfOpen; 69 if ( !isBoolean( opts.allowHalfOpen ) ) { 70 return new TypeError( 'invalid option. `allowHalfOpen` option must be a primitive boolean. Option: `' + opts.allowHalfOpen + '`.' ); 71 } 72 } 73 if ( hasOwnProp( options, 'highWaterMark' ) ) { 74 opts.highWaterMark = options.highWaterMark; 75 if ( !isNonNegative( opts.highWaterMark ) ) { 76 return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' ); 77 } 78 } 79 return null; 80 } 81 82 83 // EXPORTS // 84 85 module.exports = validate;