Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge the array-style and parameter-style args? #9

Closed
fzembow opened this issue Mar 26, 2015 · 1 comment
Closed

Merge the array-style and parameter-style args? #9

fzembow opened this issue Mar 26, 2015 · 1 comment

Comments

@fzembow
Copy link
Collaborator

fzembow commented Mar 26, 2015

Consider the following:

var clues = require("clues");

var logic = {

  user: {
    name: "Meow Zedong",
  },

  answer: 42,

  message: ['user.name', function(name, answer) {
    return name + ", the answer is: " + answer;
  }]
};

clues(logic, 'message').then(function(res){
  console.log(res);
});

This results in:

Meow Zedong, the answer is: undefined

Obviously this will work as we expect if the array-style arguments also include answer ... but do you think that the positional arguments in the array could be extended by the arguments in the function?

I found that when using reptiles with some user input I end up seeing something like

var api = {
  something: ['input.A', 'B', 'C', 'D', 'E', function(a, b, c, d, e){
    ...
  }
}

... and it feels very verbose when it's just the first variable that needs the object resolution. Would be much nicer if it were:

var api = {
  something: ['input.A', function(a, b, c, d, e){
    ...
  }
}
@ZJONSSON
Copy link
Owner

Excellent idea, could be something like this

// Support an array with argument names in front and the function as last element
    if (typeof fn === 'object' && fn.length && typeof fn[fn.length-1] == 'function') {
      args = fn.slice(0,fn.length-1);
      fn = fn[fn.length-1];
      matchArgs(fn).forEach(function(d,i) {
        if (args[i] === undefined) args[i] = d;
      });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants