validate.js (3192B)
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; 27 var isNonNegative = require( '@stdlib/assert/is-nonnegative-number' ).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 {boolean} [options.objectMode] - specifies whether a stream should operate in object mode 39 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false` 40 * @param {boolean} [options.decodeStrings] - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream 41 * @param {string} [options.defaultEncoding] - default encoding when not explicitly specified when writing data 42 * @returns {(Error|null)} null or an error object 43 * 44 * @example 45 * var opts = {}; 46 * var options = { 47 * 'objectMode': true 48 * }; 49 * 50 * var err = validate( opts, options ); 51 * if ( err ) { 52 * throw err; 53 * } 54 */ 55 function validate( opts, options ) { 56 if ( !isObject( options ) ) { 57 return new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' ); 58 } 59 if ( hasOwnProp( options, 'objectMode' ) ) { 60 opts.objectMode = options.objectMode; 61 if ( !isBoolean( opts.objectMode ) ) { 62 return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' ); 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 if ( hasOwnProp( options, 'decodeStrings' ) ) { 72 opts.decodeStrings = options.decodeStrings; 73 if ( !isBoolean( opts.decodeStrings ) ) { 74 return new TypeError( 'invalid option. `decodeStrings` option must be a primitive boolean. Option: `' + opts.decodeStrings + '`.' ); 75 } 76 } 77 if ( hasOwnProp( options, 'defaultEncoding' ) ) { 78 opts.defaultEncoding = options.defaultEncoding; 79 if ( !isString( opts.defaultEncoding ) ) { 80 return new TypeError( 'invalid option. `defaultEncoding` option must be a primitive string. Option: `' + opts.defaultEncoding + '`.' ); 81 } 82 } 83 return null; 84 } 85 86 87 // EXPORTS // 88 89 module.exports = validate;