Skip to content

Commit

Permalink
Merge pull request jashkenas#1917 from tjbarber/avoid-returning-zero-…
Browse files Browse the repository at this point in the history
…truthy-isSorted

Fixes jashkenas#1870: _.indexOf with `isSorted` of `true` will incorrectly match `undefined` on an empty array.
  • Loading branch information
jashkenas committed Nov 4, 2014
2 parents 2115d56 + 43dbb7d commit 3397cad
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@
strictEqual(_.indexOf(array, 1, fromIndex), 0);
});
strictEqual(_.indexOf([1, 2, 3], 1, true), 0);

index = _.indexOf([], undefined, true);
equal(index, -1, 'empty array with truthy `isSorted` returns -1');
});

test('lastIndexOf', function() {
Expand Down
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@
var i = 0, length = array && array.length;
if (typeof isSorted == 'number') {
i = isSorted < 0 ? Math.max(0, length + isSorted) : isSorted;
} else if (isSorted) {
} else if (isSorted && length) {
i = _.sortedIndex(array, item);
return array[i] === item ? i : -1;
}
Expand Down

0 comments on commit 3397cad

Please sign in to comment.