1.0.1 • Published 5 years ago
must-lookup-prop-in-dict-pmb v1.0.1
must-lookup-prop-in-dict-pmb
Look up the value of some config object's property in a dictionary object, throw if not found. (Double lookup, double throw.)
API
This module exports one function:
lookup(descr, cfg, dict, key, dflt)
First, look up the option value, i.e. cfg[key] if it exists,
otherwise dflt will be used as the option value.
- The idea is that the option value is something that makes sense to a
human reader, e.g.
presentation. - The option value cannot be
undefined(the false-y value). This should not be a problem, as it would be stringified fordictlookup anyway, so you can use the string'undefined'. - If
cfgis a function, the option value will instead be determined bycfg(key, dflt). A result ofundefinedmeans failure.- This feature works nicely with the
.ifHasfrom objpop.
- This feature works nicely with the
Once the option value is determined, translate it by dict to an
actionable representation, and return that, e.g.
{ HandleLidSwitch: 'ignore', IdleAction: 'ignore' }.
If either lookup fails, throw an error.
The error message will use String(descr || dict) as the (hopefully)
human-readable context description.
Usage
see the tests.
Known issues
- Needs more/better tests and docs.
License
ISC