From 6b7c1ad6cf1b1f48dbc119f87808ea9879a0bb64 Mon Sep 17 00:00:00 2001 From: jdalton Date: Thu, 25 Jun 2015 14:34:04 -0700 Subject: [PATCH] Ensure `_.indexOf` and `_.lastIndexOf` return `-1` when performing a binary search for `undefined` and it's not found. [closes #1303] --- lodash.src.js | 7 +++---- test/test.js | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 2692c43ec..5d8510da8 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -5089,10 +5089,9 @@ if (typeof fromIndex == 'number') { fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex; } else if (fromIndex) { - var index = binaryIndex(array, value), - other = array[index]; - - if (value === value ? (value === other) : (other !== other)) { + var index = binaryIndex(array, value); + if (index < length && + (value === value ? (value === array[index]) : (array[index] !== array[index]))) { return index; } return -1; diff --git a/test/test.js b/test/test.js index f91e09f77..5d9f223f6 100644 --- a/test/test.js +++ b/test/test.js @@ -9354,12 +9354,13 @@ deepEqual(actual, expected); }); - test('`_.' + methodName + '` should return `-1` for an unmatched value', 4, function() { + test('`_.' + methodName + '` should return `-1` for an unmatched value', 5, function() { var array = [1, 2, 3], empty = []; strictEqual(func(array, 4), -1); strictEqual(func(array, 4, true), -1); + strictEqual(func(array, undefined, true), -1); strictEqual(func(empty, undefined), -1); strictEqual(func(empty, undefined, true), -1);