Created
May 19, 2014 05:03
-
-
Save graue/5b734259e9de79626925 to your computer and use it in GitHub Desktop.
Function to warn if unexpected keys are in an object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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