From dc9b320c4addbb91277ec2074cdcbe35af6ef73e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 25 Sep 2014 09:42:27 -0700 Subject: [PATCH] Ensure `_.indexOf` and `_.lastIndexOf` match `NaN` when performing a binary search. --- lodash.js | 9 ++++++--- test/test.js | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index b1ba43f2d..6871ef29b 100644 --- a/lodash.js +++ b/lodash.js @@ -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); diff --git a/test/test.js b/test/test.js index 383abeb94..f1cc2c464 100644 --- a/test/test.js +++ b/test/test.js @@ -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); }); });