Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 999 Bytes

prefer-identity.md

File metadata and controls

50 lines (33 loc) · 999 Bytes

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.

Options

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}]

Fail

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; };

Pass

function foo(y, x) {
  return x;
}

_.filter(_.identity, array);

// When not including arrow functions
_.filter((x) => x, array);

When Not To Use It

If you do not want to enforce using _.identity, you should not use this rule.