-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
_.any can return incorrect results when native forEach functions are used #177
Comments
The is issue exists but the fix is partially incorrect, the original code reads : if (result = iterator.call(context, value, index, list)) return breaker; not if (result == iterator.call(context, value, index, list)) return breaker; which does override |
Yes, I mistyped the original code, but my fix is correct. If |
Sorry I misread |
Already commented this on the pull request -- but this needs to have some test cases added. |
This should be fixed as of the corresponding pull request. |
I dig this patch/issue... I'm curious under what circumstances did this bug happen (an object passed the has native forEach(), but not some())? |
Would happen if your environment doesn't support neither forEach nor some, and you add forEach manually by extending Array.prototype, then use .any/.some. |
Repeating the question from @jdalton ( but modified ), from 2 years ago, are there any cases when a native forEach is present but a native some is not present, AND the prototype chain has not been user modified to effect this? That is, a browser has implemented the language this way? |
@chrisaaaker It's for when a 3rd-party lib shims one native array method and not another. |
When a native forEach function is present, if
_.any
is called with a list in which the last item does not pass the truth test it will always returnfalse
(even if any other items in the list passed the truth test). The problem is that the native forEach function does not short circuit (return breaker;
has no effect on it). Therefore, the return result for the call to_.any
will be overwritten by the last item.Suggested fix:
Replace
with
in
_.any
, resulting in:This prevents
result
from being set totrue
and then later being overwritten byfalse
.Edit: because the
|=
operation returns an integer, to maintain a strictly boolean return type, the return value would need to be!!result
The text was updated successfully, but these errors were encountered: