Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of arguments of a function, in javascript, INCLUDING optional arguments? [duplicate]

For example, if I have the following function in javascript:

var f1 = function(a, b, c) {}
console.log(f1.length); // 3

But with this function, the output is different:

var f2 = function(a, b, c = 6) {}
console.log(f2.length); // 2

How do I count number of arguments of f2 INCLUDING optional arguments?

like image 856
Pedro Furtado Avatar asked Aug 14 '18 21:08

Pedro Furtado


People also ask

How do you find the number of arguments in JavaScript?

The arguments. length property contains the number of arguments passed to the function.

How do you count arguments in a function?

Syntax *args allow us to pass a variable number of arguments to a function. We will use len() function or method in *args in order to count the number of arguments of the function in python.

Does JavaScript functions check for the number of arguments received?

Parameter Rules JavaScript function definitions do not specify data types for parameters. JavaScript functions do not perform type checking on the passed arguments. JavaScript functions do not check the number of arguments received.

How can you get the total number of arguments passed to a function using args length property using arguments length property both of the above None of the above?

Explanation. Using arguments. length property, we can get the total number of arguments passed to a function.


1 Answers

Well, it's a bit of a mess but I believe this should cover most edge cases.

It works by converting the function to a string and counting the commas, but ignoring commas that are in strings, in function calls, or in objects/arrays. I can't think of any scenarios where this won't return the proper amount, but I'm sure there is one, so this is in no way foolproof, but should work in most cases.

function getNumArgs(func) {
  var funcStr = func.toString();
  var commaCount = 0;
  var bracketCount = 0;
  var lastParen = 0;
  var inStrSingle = false;
  var inStrDouble = false;
  for (var i = 0; i < funcStr.length; i++) {
    if (['(', '[', '{'].includes(funcStr[i]) && !inStrSingle && !inStrDouble) {
      bracketCount++;
      lastParen = i;
    } else if ([')', ']', '}'].includes(funcStr[i]) && !inStrSingle && !inStrDouble) {
      bracketCount--;
      if (bracketCount < 1) {
        break;
      }
    } else if (funcStr[i] === "'" && !inStrDouble && funcStr[i - 1] !== '\\') {
      inStrSingle = !inStrSingle;
    } else if (funcStr[i] === '"' && !inStrSingle && funcStr[i - 1] !== '\\') {
      inStrDouble = !inStrDouble;
    } else if (funcStr[i] === ',' && bracketCount === 1 && !inStrSingle && !inStrDouble) {
      commaCount++;
    }
  }

  // Handle no arguments (last opening parenthesis to the last closing one is empty)
  if (commaCount === 0 && funcStr.substring(lastParen + 1, i).trim().length === 0) {
    return 0;
  }

  return commaCount + 1;
}

Here are a few tests I tried it on: https://jsfiddle.net/ekzuvL0c/

like image 73
Mogzol Avatar answered Oct 02 '22 22:10

Mogzol