keyblade
Fail fast when accessing undefined properties on objects.
Installation
This is not the library you need. This is the library you deserve!
npm install keyblade --save
Requires Node v6 or above.
Usage
const keyblade UndefinedKeyError = // Object to protectconst unsafe = hello: 'world' console// < 'world'console// < undefined // Create a protected version (does not modify `unsafe`)const safe = console// < 'world'console// < UndefinedKeyError: The key 'goodbye' does not exist on the object.
Note: to ensure interoperability with utilities that use Symbols, checking them has been disabled. This means using util.inspect
and JSON.stringify
works without issues. I don't know why you would need to stringify a protected object but... you can!
need deserve it?
Why do I Glad you asked! Heard of those wonderful things we call environment variables? They're fun! Even more so when someone forgets to define them.
const env = processenv const cert = // Later... // < Error: something about strings or buffers I dunno man..
Of course, you could be all like..
if !envCERT_FILE_PATH throw 'No CERT_FILE_PATH specified'
Now repeat that 43 times. Or, you could use keyblade
!
const keyblade = const env = const cert = // < UndefinedKeyError: The key 'CERT_FILE_PATH' does not exist on the object.
One could even get fancy and customize the error message.
const env = const cert = // < UndefinedKeyError: Environment variable CERT_FILE_PATH is not set.
If you are not a fan of the console.error
happening before throwing, you can either customize it:
const env =
Or disable it entirely.
const env =
Contributing
npm run
scripts
npm run test
: Runs tests oncenpm run test-watch
: Runs tests in watch-modenpm run lint
: Lints the code oncenpm run lint-watch
: Lints the code in watch-modenpm run cover
: Runs code coverage usingnyc
(istanbul
)npm run coveralls
: Used by coveralls
Author
Jeff Hansen - @Jeffijoe