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