multiple arguments and conditions in a function in javascript

Solution 1:

Cyclomatic complexity is just a style suggestion that ESLint provides to encourage you to use fewer code branches (e.g. ifs) in a single function. You can ignore the warning or disable it with ESLint pragmas (the rule in question is named complexity).

If you want a single function that is able to query in different ways (as you're doing in your code), it stands to reason that your function will need to branch out based on the input data.

For example, Python functions do this all the time by querying which kwargs were and weren't supplied to an "overloaded" function and the function changes behavior based on the suppied args. I'm not sure why your ESLint is configured with such a low complexity value, or maybe it comes with a low value.

Last time I used ESLint I found myself disabling three or four "code style" suggestions right away. I personally think it's overly opinionated by default.

Solution 2:

Perhaps you can do like;

var employee = o.id   ? employees.find(e => e.id === o.id) :
               o.name ? employees.find(e => e.lastName === o.name || e.firstName === o.name)
                      : undefined;

So if ESLint complains for nested ternary perhaps you may try;

var employee = (o.id   && employees.find(e => e.id === o.id)) ||
               (o.name && employees.find(e => e.lastName === o.name || e.firstName === o.name));

and if all fails you get false.