Getting the number of parameters a function expects

I guess when you do things like this with your parameters, you possibly need a parser.

const myDaftFunction = (
  n, 
  greet = function( 
    { greeting = 'Hello', name = 'Bob' } = {},
    fn = (str) => str.toUpperCase(),
    sep = ' '
  ) {
    return fn(greeting + sep + name)
  }
) => {
  return greet({name: 'Fred!'}).repeat(n)
}

console.log(myDaftFunction(3)) // "HELLO FRED!HELLO FRED!HELLO FRED!"

I would also add that if I was writing my own framework, that using something like Acorn would make perfect sense.

It’s been interesting looking into this though :slight_smile:

No, you need an intervention.

3 Likes

I guess when you can do things like this with your parameters…

Sorry I missed an important word from that sentence. I was kind of making the point that there maybe edge cases where a function like mine in #1 is going to break. A parser being more robust.

I did call it myDaftFunction for a reason.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.