Skip to content

Latest commit

 

History

History
94 lines (81 loc) · 1.67 KB

prefer-composition-grouping.md

File metadata and controls

94 lines (81 loc) · 1.67 KB

Prefer grouping similar methods in composition methods

When using composition methods like _.flow or _.compose, it can be preferred to regroup similar methods in a new composition method call. The methods for which this rule will create an error are _.map, _.filter and _.reject. When using _.filter and _.reject and shorthands, you might want to use _.overEvery and _.overSome. When using the _.map shorthand, you might want to use _.get.

_.flow(
  _.map(fn1),
  _.map(fn2)
)(a);
// -->
_.map(
  _.flow(fn1, fn2)
)(a);

_.flow(
  _.map('property'), // _.pluck deprecated
  _.map(fn2)
)(a);
// -->
_.map(
  _.flow(_.get('property'), fn2)
)(a);

_.flow(
  _.map(fn1),
  _.map(fn2),
  _.filter(fn3)
)(a);
// -->
_.flow(
  _.map(_.flow(fn1, fn2)),
  _.filter(fn3)
)(a);

_.flow(
  _.filter(fn1),
  _.filter('property'),
  _.map(fn2)
)(a);
// -->
_.flow(
  _.filter(_.overEvery([fn1, 'property'])),
  _.map(fn2)
)(a);

_.flow(
  _.reject(fn1),
  _.reject('property'),
  _.map(fn2)
)(a);
// -->
_.flow(
  _.reject(_.overSome([fn1, 'property'])),
  _.map(fn2)
)(a);

Fail

_.flow(
  _.map(fn1),
  _.map(fn2)
)(a);

_.flow(
  _.map(fn1),
  _.map(fn2),
  _.filter(fn3)
)(a);

Pass

_.map(
  _.flow(fn1, fn2)
)(a);

_.flow(
  _.map(_.flow(fn1, fn2)),
  _.filter(fn3)
)(a);