an argument validator [originally] for
yargs
import argsert from 'argsert';
let passed;
try {
passed = argsert([configurationString], arguments);
} catch (err) {
if (err instanceof TypeError) {
// a type was missing or incorrectly given
} else {
// otherwise, there was something wrong with the configuration
// or the number of args passed in.
}
}
import argsertPromise from 'argsert/promise';
argsertPromise([configurationString], arguments)
.then(passed => passed)
.catch(err => {
// same error as above
});
array
boolean
buffer
error
function
null
number
object
promise
[that passes then/is-promise]string
symbol
undefined
*
wildcard:allows for any type.
configurationString
space-separated entries with the following syntax:
[optional arguments]
'[string|number] [object]'
:
<required arguments>
'<object> <*>'
:
The following leverages argsert
best because it is the most performant and easiest to read by:
argsert
's this
to the configurationString
function nodeStyleCallback(err, result) {
// ...
argsert.apply('<error|undefined|null> [object|string]', arguments);
// ...
}
You can also look at the example tests for other ways to invoke argsert
.
To learn more you can read about JS' function methods: apply
, bind
, and call
.