validate.js (3967B)
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 var isRegExp = require( '@stdlib/assert/is-regexp' ); 29 30 31 // MAIN // 32 33 /** 34 * Validates function options. 35 * 36 * @private 37 * @param {Object} opts - destination object 38 * @param {Options} options - function options 39 * @param {(string|RegExp)} [options.sep] - separator used to split streamed data 40 * @param {boolean} [options.objectMode] - specifies whether stream should operate in object mode 41 * @param {(string|null)} [options.encoding] - specifies how `Buffer` objects should be decoded to `strings` 42 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false` 43 * @param {boolean} [options.allowHalfOpen] - specifies whether the stream should remain open even if one side ends 44 * @param {boolean} [options.writableObjectMode] - specifies whether the writable side should be in object mode 45 * @returns {(Error|null)} null or an error object 46 * 47 * @example 48 * var opts = {}; 49 * var options = { 50 * 'sep': '\t', 51 * 'objectMode': true 52 * }; 53 * var err= validate( opts, options ); 54 * if ( err ) { 55 * throw err; 56 * } 57 */ 58 function validate( opts, options ) { 59 if ( !isObject( options ) ) { 60 return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' ); 61 } 62 if ( hasOwnProp( options, 'sep' ) ) { 63 opts.sep = options.sep; 64 if ( 65 !isString( opts.sep ) && 66 !isRegExp( opts.sep ) 67 ) { 68 return new TypeError( 'invalid option. `sep` option must be either a primitive string or a regular expression. Option: `' + opts.sep + '`.' ); 69 } 70 } 71 if ( hasOwnProp( options, 'objectMode' ) ) { 72 opts.objectMode = options.objectMode; 73 if ( !isBoolean( opts.objectMode ) ) { 74 return new TypeError( 'invalid option. `objectMode` option must be a boolean primitive. Option: `' + opts.objectMode + '`.' ); 75 } 76 } 77 if ( hasOwnProp( options, 'writableObjectMode' ) ) { 78 opts.writableObjectMode = options.writableObjectMode; 79 if ( !isBoolean( opts.writableObjectMode ) ) { 80 return new TypeError( 'invalid option. `writableObjectMode` option must be a boolean primitive. Option: `' + opts.writableObjectMode + '`.' ); 81 } 82 } 83 if ( hasOwnProp( options, 'encoding' ) ) { 84 opts.encoding = options.encoding; 85 if ( !isString( opts.encoding ) ) { 86 return new TypeError( 'invalid option. `encoding` option must be a string primitive. Option: `' + opts.encoding + '`.' ); 87 } 88 } 89 if ( hasOwnProp( options, 'allowHalfOpen' ) ) { 90 opts.allowHalfOpen = options.allowHalfOpen; 91 if ( !isBoolean( opts.allowHalfOpen ) ) { 92 return new TypeError( 'invalid option. `allowHalfOpen` option must be a boolean primitive. Option: `' + opts.allowHalfOpen + '`.' ); 93 } 94 } 95 if ( hasOwnProp( options, 'highWaterMark' ) ) { 96 opts.highWaterMark = options.highWaterMark; 97 if ( !isNonNegative( opts.highWaterMark ) ) { 98 return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' ); 99 } 100 } 101 return null; 102 } 103 104 105 // EXPORTS // 106 107 module.exports = validate;