warn-undefined
v1.0.0
Published
A simple wrapper around an object that will warn when you access keys that aren't defined (using the magic of ES6 Proxies)
Downloads
3
Maintainers
Readme
warn-undefined
You can use this function to wrap an object; then if you try and access a property of the object that is not defined it will log an error to the console (or throw an error, if you prefer) helping you to catch programming bugs faster.
IMPORTANT - uses ES6 Proxy!
If you aren't on a platform that supports ES6 Proxies this will fall
back to just returning the object. If you're running older versions of
node, you may need to run them with --harmony-proxies
for
warn-undefined
to make any difference.
Options
throwError
: set to true to throw errors instead of logging themwarn
: useconsole.warn
instead ofconsole.error
disabled
: set to true to bypass (e.g. on production)fallback
: changes the default return fromundefined
to whatever's provided here; :warning: only works ifProxy
is supported!
Example
var warnUndefined = require('warn-undefined');
var foo = {bar: 'baz', quux: 'fred'};
var protectedFoo = warnUndefined(foo, {
disabled: (['development', 'test'].indexOf(process.env.NODE_ENV) < 0),
throwError: true
});
foo.bar; //baz
protectedFoo.bar; //baz
foo.nx; //undefined
protectedFoo.nx; //Throws error!