Ensure _.indexOf and _.lastIndexOf match NaN when performing a binary search.

This commit is contained in:
John-David Dalton
2014-09-25 09:42:27 -07:00
parent a6a93148b4
commit dc9b320c4a
2 changed files with 8 additions and 4 deletions

View File

@@ -3683,8 +3683,10 @@
if (typeof fromIndex == 'number') {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
} else if (fromIndex) {
var index = sortedIndex(array, value);
return array[index] === value ? index : -1;
var index = sortedIndex(array, value),
other = array[index];
return (value === value ? value === other : other !== other) ? index : -1;
}
return baseIndexOf(array, value, fromIndex);
}
@@ -3821,7 +3823,8 @@
index = (fromIndex < 0 ? nativeMax(length + fromIndex, 0) : nativeMin(fromIndex || 0, length - 1)) + 1;
} else if (fromIndex) {
index = sortedLastIndex(array, value) - 1;
return array[index] === value ? index : -1;
var other = array[index];
return (value === value ? value === other : other !== other) ? index : -1;
}
if (value !== value) {
return indexOfNaN(array, index, true);

View File

@@ -7066,8 +7066,9 @@
strictEqual(func(empty, undefined, true), -1);
});
test('`_.' + methodName + '` should match `NaN`', 1, function() {
test('`_.' + methodName + '` should match `NaN`', 2, function() {
strictEqual(func([1, NaN, 3], NaN), 1);
strictEqual(func([1, 3, NaN], NaN, true), 2);
});
});