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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: prefer-array-some #736

Closed
fisker opened this issue May 17, 2020 · 13 comments 路 Fixed by #887
Closed

Rule proposal: prefer-array-some #736

fisker opened this issue May 17, 2020 · 13 comments 路 Fixed by #887

Comments

@fisker
Copy link
Collaborator

fisker commented May 17, 2020

Fail

if (array.find(element => element === '馃')) {
}
const foo = array.find(element => element === '馃') ? bar : baz;

Pass

if (array.some(element => element === '馃')) {
}
const foo = array.some(element => element === '馃') ? bar : baz;

Just experienced one few days ago, and kind of follow up for #730

@fisker
Copy link
Collaborator Author

fisker commented May 17, 2020

One note, we should definitely filter the first argument which is Literal like #704 (comment) and #697, because there is widely used jQuery.find().

Update: on second thought, jQuery.find() seems can't use in a if or ternary.

@silverwind
Copy link
Contributor

silverwind commented May 22, 2020

I wouldn't specifically suggest some but warn on the wrongly used array#find return value in the boolean context.

BTW, I did a rule to eslint proposal regarding a general unused return values rule a while ago, maybe it is worth exploring here, #741.

@papb
Copy link

papb commented May 22, 2020

I wouldn't specifically suggest some but warn on the wrongly used array#find return value in the boolean context.

I like the idea of suggesting some. Might help users who don't know any better.

BTW, I did a rule to eslint proposal regarding a general unused return values rule a while ago, maybe it is worth exploring here, #741.

@silverwind What is the connection? In this case, the return value is being used, so what exactly do you mean?

@silverwind
Copy link
Contributor

There's no real connection, my mistake. Return value is used here (thought wrongly).

@fisker fisker changed the title Rule proposal: prefer-some Rule proposal: prefer-array-some May 22, 2020
@silverwind
Copy link
Contributor

silverwind commented May 22, 2020

My issue with array#some and array#every is that I think it's hard to read/understand. I prefer for-of with a break statement because I don't have to think about whether the iterator needs to return true or false to break the loop.

@fisker
Copy link
Collaborator Author

fisker commented May 22, 2020

Why are you thinking about breaking loop? When use some and every you should think is this element/result I'm searching? One important thing you should think about is, if it's empty, what the result will be.

@silverwind
Copy link
Contributor

Yeah it might just be me and some/every are fine. As an example, I'd prefer the second variant here because it just seems easier to read to me:

function hasBiggerThan10(array) {
  return array.some(element => element > 10);
}

function hasBiggerThan10(array) {
  for (const element of array) {
    if (element > 10) return true;
  }
  return false;
}

@fisker
Copy link
Collaborator Author

fisker commented May 22, 2020

const isBiggerThan10 = element => element > 10;

function hasBiggerThan10(array) {
  return array.some(isBiggerThan10);
}

function allBiggerThan10(array) {
  return array.every(isBiggerThan10);
}

Readable to me , and I don't see why should think about breaking loop.

@silverwind
Copy link
Contributor

silverwind commented May 22, 2020

Maybe I've expressed myself badly but the "hard" part is remembering to return a truthy value in the callback.

One distinct advantage for-of has over array methods is that it works on all iterables (NodeList, Set, Map), so it's a win-win to use it over array methods.

@fisker
Copy link
Collaborator Author

fisker commented May 22, 2020

Yes, I agree in some case loop better, some also useful to avoid use let/break in simple tests. I use both.

@riophae
Copy link

riophae commented May 24, 2020

Although this could be a taste issue, Array#some can easily run on all iterables too. Just FYI.

Array.from(new Set([ 1, 2, 3, 2 ])).some(...)
Array.from('abc').some(...)
Array.from(document.querySelectorAll('div')).some(...)

@sindresorhus
Copy link
Owner

This is accepted.

@fisker
Copy link
Collaborator Author

fisker commented Oct 21, 2020

WIP

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

Successfully merging a pull request may close this issue.

5 participants