Prefer _.identity
over functions returning their argument
When you want a function that takes one argument and always returns the argument directly, it can be more concise to use _.identity
.
This rule supports the following options:
arrowFunctions
: Whether or not to to report arrow functions. Default is false
, meaning arrow functions will not get reported.
You can set the options like this:
"lodash-fp/prefer-identity": ["error", {"arrowFunctions": true}]
function id(x) {
return x;
}
_.filter(function id(x) {
return x;
}, array);
// When including arrow functions
const id = (x) => x;
const id = (x) => { return x; };
function foo(y, x) {
return x;
}
_.filter(_.identity, array);
// When not including arrow functions
_.filter((x) => x, array);
If you do not want to enforce using _.identity
, you should not use this rule.