Skip to content

Instantly share code, notes, and snippets.

@graue
Created May 19, 2014 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graue/5b734259e9de79626925 to your computer and use it in GitHub Desktop.
Save graue/5b734259e9de79626925 to your computer and use it in GitHub Desktop.
Function to warn if unexpected keys are in an object
// Shameless yak-shaving. I don't even remotely need this for
// anything right now, but wrote it due to a lament that keys
// in JavaScript {options: 'objects'} aren't checked for
// correctness and typos/misspelling can easily go unnoticed.
var _ = require('underscore');
// Expect all keys in the object to be from the list in "allowed".
// Emit a warning if they don't.
// TODO: Should compile down to nothing in production as this is
// only to debug.
function expectKeys(obj, allowed) {
if (allowed && allowed.length >= 1) {
var toWarnAbout = [];
for (var k in obj) {
if ({}.hasOwnProperty.call(obj, k) && ~allowed.indexOf(k)) {
toWarnAbout.push(k);
}
}
if (toWarnAbout.length) {
console.warn('object has unexpected key: ' + toWarnAbout.join(', '));
console.trace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment