-
-
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
Fixed bug in _.sortedIndex that put undefined at the start (FIX: #1834) #2042
Conversation
var sorted = _.sortBy([undefined, 1, undefined, 2]); | ||
equal(_.sortedIndex(sorted, undefined, _.identity), 2); | ||
|
||
var edgeCaseNumbers = [-Infinity, -Infinity, 0, Number.MAX_VALUE, Infinity, Infinity, undefined, undefined, NaN]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number.MAX_VALUE won't exist in all environments. Other than that we'll have to discuss the implications. May be worth while to consider #1882 at the same time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number.MAX_VALUE
is like ES3 or smth it should be supported everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated Number.MAX_VALUE
to be just a really big number. What do you think of the rest of it?
@@ -685,7 +685,8 @@ | |||
var low = 0, high = array.length; | |||
while (low < high) { | |||
var mid = Math.floor((low + high) / 2); | |||
if (iteratee(array[mid]) < value) low = mid + 1; else high = mid; | |||
var cur = iteratee(array[mid]); | |||
if ( cur < value || (value === void 0 && cur <= Infinity) || (_.isNaN(value) && (cur <= Infinity || cur === void 0))) low = mid + 1; else high = mid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been pretty busy the past couple weeks (exam time) but I'm playing with this logic now. I've almost got it passing (1 failing test) with this
if ( cur < value || (cur <= Infinity && !(value <= value)) ) low = mid + 1;
else high = mid;
Hows this look @jdalton https://github.com/jashkenas/underscore/compare/master...megawac:pr/2042?expand=1 (I don't recall why I stopped working on this branch) |
Changed _.sortedIndex function to hangle undefined values correctly (FIXES: #1834 )
Revised previous pull request: #2041
Previously
Changed it to:
The logic works because Undefined should be placed after the highest number possible + 1.
Based on @jdalton request in #2041 (comment) I added
NaN
to always be placed after undefined.Based off of #1768 (comment)
The code aboves logic checks to see if the value is NaN, if it is go past
Infinity
orundefined
.So that how I built:
Since no numeric value can be greater than Infinity, it will place undefined values to the correct index.
And it will place all NaN past
Infinity
orundefined
.Also added edgeCaseTests, to verify it works: