0

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?

  • This question is right up your alley - using a string to call its function equivalent (without the atrocity that is eval) http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call – James LeClair Sep 08 '13 at 23:25

2 Answers2

1

Adapting the answer from the link I put in comment above and applying it to your situation:

your functions.js file would contain the following:

exports.weatherFunctions = {

    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;
    }

}

require that in your file as you tried above:

var prefs = require('./preferences.json');
var funcs = require('./functions.js')

from here you can loop through as you wish.

for (key in prefs){
  if(prefs[key]) {
    funcs.weatherFunctions[key](); 
  }
}
James LeClair
  • 1,234
  • 10
  • 12
0

You can create an object with the names of the functions as keys and the functions as values:

funcs = {
  getTemperature: function(){
    //retrieve temperature;
    //append temperature to file;
  }

  getHumidity: function(){
    //retrieve humidity;
    //append humidity to file;
  }

  getPrecipitation: function() {
    //retrieve precipitation;
    //append precipitation to file;
  }
}

If you want to put them in a separate file and use them with var funcs = require('./functions.js');, you can put:

module.exports = funcs

Now your main file should almost work as-is. You'll need to change the line funcs.key(); to funcs[key]();.

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69