validate.js (4131B)
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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 28 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 29 var isFunction = require( '@stdlib/assert/is-function' ); 30 31 32 // MAIN // 33 34 /** 35 * Validates function options. 36 * 37 * @private 38 * @param {Object} opts - destination object 39 * @param {Options} options - function options 40 * @param {string} [options.sep] - separator used to join streamed data 41 * @param {boolean} [options.objectMode] - specifies whether a stream should operate in object mode 42 * @param {(string|null)} [options.encoding] - specifies how `Buffer` objects should be decoded to `strings` 43 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the maximum number of bytes to store in the internal buffer before pausing streaming 44 * @param {Function} [options.serialize] - custom serialization function 45 * @param {NonNegativeInteger} [options.iter=1e308] - number of iterations 46 * @param {integer} [options.dir] - iteration direction 47 * @returns {(Error|null)} null or an error object 48 * 49 * @example 50 * var opts = {}; 51 * var options = { 52 * 'objectMode': true 53 * }; 54 * var err= validate( opts, options ); 55 * if ( err ) { 56 * throw err; 57 * } 58 */ 59 function validate( opts, options ) { 60 if ( !isObject( options ) ) { 61 return new TypeError( 'invalid argument. Options must be an object. Value: `' + options + '`.' ); 62 } 63 if ( hasOwnProp( options, 'sep' ) ) { 64 opts.sep = options.sep; 65 if ( !isString( opts.sep ) ) { 66 return new TypeError( 'invalid option. `sep` option must be a primitive string. Option: `' + opts.sep + '`.' ); 67 } 68 } 69 if ( hasOwnProp( options, 'objectMode' ) ) { 70 opts.objectMode = options.objectMode; 71 if ( !isBoolean( opts.objectMode ) ) { 72 return new TypeError( 'invalid option. `objectMode` option must be a primitive boolean. Option: `' + opts.objectMode + '`.' ); 73 } 74 } 75 if ( hasOwnProp( options, 'encoding' ) ) { 76 opts.encoding = options.encoding; 77 if ( !isString( opts.encoding ) && opts.encoding !== null ) { 78 return new TypeError( 'invalid option. `encoding` option must be a primitive string or null. Option: `' + opts.encoding + '`.' ); 79 } 80 } 81 if ( hasOwnProp( options, 'highWaterMark' ) ) { 82 opts.highWaterMark = options.highWaterMark; 83 if ( !isNonNegative( opts.highWaterMark ) ) { 84 return new TypeError( 'invalid option. `highWaterMark` option must be a nonnegative number. Option: `' + opts.highWaterMark + '`.' ); 85 } 86 } 87 if ( hasOwnProp( options, 'serialize' ) ) { 88 opts.serialize = options.serialize; 89 if ( !isFunction( opts.serialize ) ) { 90 return new TypeError( 'invalid option. `serialize` option must be a function. Option: `' + opts.serialize + '`.' ); 91 } 92 } 93 if ( hasOwnProp( options, 'iter' ) ) { 94 opts.iter = options.iter; 95 if ( !isNonNegativeInteger( opts.iter ) ) { 96 return new TypeError( 'invalid option. `iter` option must be a nonnegative integer. Option: `' + opts.iter + '`.' ); 97 } 98 } 99 if ( hasOwnProp( options, 'dir' ) ) { 100 opts.dir = options.dir; 101 if ( opts.dir !== 1 && opts.dir !== -1 ) { 102 return new TypeError( 'invalid option. `dir` option must be either `1` or `-1`. Option: `' + opts.dir + '`.' ); 103 } 104 } 105 return null; 106 } 107 108 109 // EXPORTS // 110 111 module.exports = validate;