My application has a list of preferences, stored in a JSON file (preferences.json). I have a list of corresponding functions that I would like to selectively use based on the preferences. The result of each function will be added to a report.
preferences.json looks something like this:
{
"getTemperature": true;
"getHumidity": false;
"getPrecipitation": true
}
The functions are in an module I'm importing (functions.js). They something like this:
var getTemperature = function(){
//retrieve temperature;
//append temperature to file;
}
var getHumidity = function(){
//retrieve humidity;
//append humidity to file;
}
var getPrecipitation = function() {
//retrieve precipitation;
//append precipitation to file;
}
What I've Tried So Far, Obviously Not Working
var prefs = require('./preferences.json');
var funcs = require('./functions.js');
for (key in prefs){
if(prefs[key]) {
funcs.key(); // <- Doesn't work, b/c 'key()' isn't a function. But you get the idea.
}
}
If you know how to accomplish this, please let me know.
One idea I haven't tried yet (it would require a good deal of code re-write) is to nest the functions in a pseudo-class along with the preference dummy variables. I'd then create an instance of the pseudo-class using the preference file. My thinking is that the pseudo-class instance would have the preferences intact and I could hard code selective execution into each function (i.e. if(myInstance.tempBool){myInstance.getTemperature()} ). I'd rather iterate though, as there are many more functions and I'll probably add more in the future.
Any thoughts?