GitHub - yargs/argsert: another argument parser, [originally] for yargs
an argument validator [originally] for
yargs
API
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. } }
Promise API
import argsertPromise from 'argsert/promise'; argsertPromise([configurationString], arguments) .then(passed => passed) .catch(err => { // same error as above });
Available Types
arraybooleanbuffererrorfunctionnullnumberobjectpromise[that passes then/is-promise]stringsymbolundefined
* wildcard:
allows for any type.
Configuration via configurationString
space-separated entries with the following syntax:
[optional arguments]
'[string|number] [object]':
- the first argument can be a string OR a number, or undefined
- the second argument can be an object literal, or undefined.
<required arguments>
'<object> <*>':
- the first argument must be an object literal
- the second argument can be any type, but must be provided.
Live Example
The following leverages argsert best because it is the most performant and easiest to read by:
setting 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.