22samuelk 22samuelk - 4 months ago 10
Javascript Question

JavaScript / Node.js: function returning another function based on parameters

Here's what I'm trying to achieve:

When the Node.js application loads, many different things are set up, and e.g.

requires.has('message', 'text')
is called. This should set up a function that checks if the passed data object contains
message.text
.

The following code registers an handler (The syntax is bot.register(callback, requirements)).

bot.register(irrelevant, [requires.has('message', 'text')]);


When a http request comes in, all requirements are executed: So, a callback must be generated from
requires.has('message', 'text')
.

So, in short:

requires.has('message', 'text')


must return

{
callable: function(data) { // check if data has nodes message and text }
}


Sorry if that explanation sounds confusing, I hope you know what I mean.
Thanks in advance!

Iso Iso
Answer

So what you want to do is a function that returns another function as an object key.

requires.has = function(message, text) {
  return {
    callable: function (data) {
      // your implementation, has access to data, message & text variables
    }
  };
};
Comments