From f7aab029784880a508a3dc437380f8dd278f02e9 Mon Sep 17 00:00:00 2001 From: Johann Steinbrecher Date: Tue, 14 Jan 2014 12:55:06 -0800 Subject: [PATCH] Fixing indexOf([], undefined, true) to return -1 insead 0 --- lodash.js | 2 +- test/test.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index 21de673e2..f99f841f4 100644 --- a/lodash.js +++ b/lodash.js @@ -2404,7 +2404,7 @@ fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0); } else if (fromIndex) { var index = sortedIndex(array, value); - return array[index] === value ? index : -1; + return (array[index] === value && array.length ? index : -1); } return baseIndexOf(array, value, fromIndex); } diff --git a/test/test.js b/test/test.js index bbe36e3a4..939aeecd2 100644 --- a/test/test.js +++ b/test/test.js @@ -3165,8 +3165,9 @@ equal(_.indexOf(array, 3), 2); }); - test('should return `-1` for an unmatched value', 1, function() { + test('should return `-1` for an unmatched value', 2, function() { equal(_.indexOf(array, 4), -1); + equal(_.indexOf([], undefined, true), -1); }); test('should work with a positive `fromIndex`', 1, function() { @@ -3197,6 +3198,7 @@ test('should work with `isSorted`', 1, function() { strictEqual(_.indexOf([1, 2, 3], 1, true), 0); }); + }()); /*--------------------------------------------------------------------------*/