Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 1.36 KB

prefer-lodash-method.md

File metadata and controls

62 lines (45 loc) · 1.36 KB

Prefer Lodash method

When using native functions like forEach and map, it's often better to use the Lodash implementation.

Rule Details

This rule takes one argument - an optional options object. This object can have two keys: except and ignoreObjects, both optional.

  • except contains an array of methods that should not be reported on.
  • ignoreObjects contains an array of objects that should not be reported on.
  • ignorePatterns contains an array of regular expressions for objects that should not be reported on

For instance, if you do not wish to use _.keys but prefer Object.keys, the config would be:

{
  "rules": {
    "lodash/prefer-lodash-method": [2, {"except": ["keys"]}]
  }
}

If you do not with the rule to work on any object named fp:

{
  "rules": {
    "lodash/prefer-lodash-method": [2, {"ignoreObjects": ["fp"]}]
  }
}

And if you don't want the rule to work on any object starting with $:

{
  "rules": {
    "lodash/prefer-lodash-method": [2, {"ignorePatterns": ["^\$[a-zA-Z0-9\_]+"]}]
  }
}

The following patterns are considered warnings:

var b = a.map(f);

if (arr.some(f)) {
  // ...
}

The following patterns are not considered warnings:

_.map(a, f);

 _(arr).map(f).reduce(g);

When Not To Use It

If you do not want to enforce using Lodash methods, you should not use this rule.