Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid-reverse is wrong #68

Closed
fregante opened this issue Jun 2, 2020 · 1 comment
Closed

avoid-reverse is wrong #68

fregante opened this issue Jun 2, 2020 · 1 comment

Comments

@fregante
Copy link

fregante commented Jun 2, 2020

Readme example:

const sum = array.reverse().reduce((p, c) => p + c, 0);
const reverseSum = array.reverse().reduceRight((p, c) => p + c, 0);

The sums are a bad example since their order doesn't matter. Try this:

['a','b','c'].reverse().reduce((p, c) => p + c, '');
// 'cba'
['a','b','c'].reverse().reduceRight((p, c) => p + c, '');
// 'abc'
['a','b','c'].reduce((p, c) => p + c, '');
// 'abc'
['a','b','c'].reduceRight((p, c) => p + c, '');
// 'cba'

Did you mean to replace .reverse().reduce() with .reduceRight() and vice-versa instead?

@freaktechnik
Copy link
Owner

I will admit that the readme examples aren't always great in showing the full behavior, their main intent is to show the syntax that triggers a rule, since the rules do not do any behavioral analysis of the callback.

The rule does indeed suggest to use reduceRight instead of reverse().reduce() and reduce instead of reverse().reduceRight().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants