Skip to content

Latest commit

 

History

History
79 lines (46 loc) · 2.01 KB

chain-style.md

File metadata and controls

79 lines (46 loc) · 2.01 KB

Chain Style

There are two ways to create a lodash chain: implicit chaining and explicit chaining with the _.chain method.

To use implicit chaining, you can call _(value) on your value and, and then finish the chain with the .value() method or with any method that returns a single value (e.g. .first(), .max()) For example:

var maxFiltered = _(arr).filter(someFilter).max(someCallback);

In order to keep the value wrapped in the chain after any single-value method, chaining needs to be explicit, with the _.chain() method. For example:

var mergedFilteredMax = _.chain(arr).filter(someFilter).max(someCallback).assign(obj).value();

For more information, check out the Lodash documentation for chaining.

Rule Details

This rule takes one argument, the preferred style: implicit, explicit or as-needed. (default is as-needed).

The following patterns are considered problems:

/*eslint lodash/chain-style: [2, "as-needed"]*/

_.chain(val).map(f).filter(g).value(); // Unnecessary explicit chaining

_.chain(val).map(f).join(c).value(); // Unnecessary explicit chaining, the chain-breaking method join() is last in the chain.
/*eslint lodash/chain-style: [2, "implicit"]*/

_.chain(val).map(f).filter(g).value(); // Do not use explicit chaining

_.chain(val).map(f).first().assign(obj).value(); // Do not use explicit chaining
/*eslint lodash/chain-style: [2, "explicit"]*/

_(val).map(f).filter(g).value(); // Do not use implicit chaining

The following patterns are not considered warnings:

/*eslint lodash/chain-style: [2, "as-needed"]*/

_(val).map(f).filter(g).value(); 

_.chain(val).map(f).first().assign(obj).value();
/*eslint lodash/chain-style: [2, "implicit"]*/

_(val).map(f).filter(g).value();
/*eslint lodash/chain-style: [2, "explicit"]*/

_.chain(val).map(f).filter(g).value(); 

When Not To Use It

If you do not want to enforce a specific chain style, then you can disable this rule.