stdopt
Wrap and validate optional values
Usage
This package provides wrappers to check inputs and provide fallbacks. There is a general some
wrapper to check if a value is defined or not, as well as a handful of others for various primitives. The chained syntax is more verbose than the standard JS way (i.e. var str = opts.str || 'default'
), but it is more solid and predictable.
var some = value // => 'some data'value // => 'other data'value // => throws error // string primitivevar string = value // => 'some text'value // => 'other text'value // => '5' // number primitivevar number = value // => 5value // => 7value // => 11 // boolean primitivevar boolean = value // => falsevalue // => truevalue // => truevalue // => false // hash primitivevar hash = value // => {stuff: true}value // => {other: false}value // => throws error // list primitivevar list = value // => [1, 2, 3]value // => ['one']value // => [4, 5, 6]value // => ['stuff']
Custom
You can also create your own custom primitives, by using the Opt
class and defining a custom static parse
method.
var Opt = { if !this instanceof lowercase return value Opt} lowercase { if typeof value === 'string' && value === value return value } value // => 'oh.'value // => 'oh.'value // => throws error
License
Apache-2.0