Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created January 13, 2016 13:51
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 Noitidart/c4ab4ca10ff5861c720b to your computer and use it in GitHub Desktop.
Save Noitidart/c4ab4ca10ff5861c720b to your computer and use it in GitHub Desktop.
_ff-addon-snippet-validateOptionsObj - What I use to validae options
function validateOptionsObj(aOptions, aOptionsDefaults) {
// ensures no invalid keys are found in aOptions, any key found in aOptions not having a key in aOptionsDefaults causes throw new Error as invalid option
for (var aOptKey in aOptions) {
if (!(aOptKey in aOptionsDefaults)) {
console.error('aOptKey of ' + aOptKey + ' is an invalid key, as it has no default value, aOptionsDefaults:', aOptionsDefaults, 'aOptions:', aOptions);
throw new Error('aOptKey of ' + aOptKey + ' is an invalid key, as it has no default value');
}
}
// if a key is not found in aOptions, but is found in aOptionsDefaults, it sets the key in aOptions to the default value
for (var aOptKey in aOptionsDefaults) {
if (!(aOptKey in aOptions)) {
aOptions[aOptKey] = aOptionsDefaults[aOptKey];
}
}
}
@Noitidart
Copy link
Author

README

Rev1

  • I use it like this:

    function(aOptions = {}) {
        var aOptionsDefaults = {
            rawr: true
        };
        validateOptionsObj(aOptions, aOptionsDefaults);
    }
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment