Lets you easily define a default value for undefined options, with optional support for Promises and lazy evaluation.
More or less the equivalent of CoffeeScript's existential operator, when used for fallback values.
## License
@ -8,9 +10,9 @@ TODO
## Donate
My income consists largely of donations for my projects. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!
Maintaining open-source projects takes a lot of time, and the more donations I receive, the more time I can dedicate to open-source. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!
You can donate using Bitcoin, PayPal, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else.
You can donate using Bitcoin, PayPal, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else. Thank you!
## Contributing
@ -22,8 +24,82 @@ Be aware that by making a pull request, you agree to release your modifications
/* `delay` will be either the value of `config.delay` or, if that
* isn't set, the value will be the result of the (hypothetical)
* database query above. The query itself will only happen if
* `config.delay` isn't set. */
});
```
## API
TODO
### defaultValue(value, fallbackValue, [options])
Returns the `value` synchronously - but when the `value` is `null` or `undefined`, it will return the `fallbackValue` instead.
* __value:__ The value you intend to use.
* __fallbackValue:__ The fallback value to use if the `value` isn't set.
* __options:__
* __evaluate:__*Defaults to `false`.* If this is set to `true`, then if either the `value` or `fallbackValue` is a function, it will be executed and its return value will be used as the value, rather than the function itself. This is especially useful in cases where the fallback value is expensive to obtain.
Equivalent to `defaultValue`, but this function will return a Promise. Similarly, `value` and `fallbackValue` may return a Promise as well, and the resulution of `value` will be awaited before deciding what to return.
The `evaluate` option is also available for this asynchronous variant, and works essentially the same - but now the evaluated functions can return a Promise as well.