Skip to content

Commit f77e099

Browse files
committed
Merge pull request #2102 from jridgewell/indexof-nan-start
Fix _.indexOf with NaN and startIndex
2 parents 8692575 + eade422 commit f77e099

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

test/arrays.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@
344344
strictEqual(_.indexOf([1, 2, NaN, NaN], NaN), 2, 'Expected [1, 2, NaN] to contain NaN');
345345
strictEqual(_.indexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, NaN] to contain NaN');
346346

347+
strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, 1), 2, 'startIndex does not affect result');
348+
strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, -2), 2, 'startIndex does not affect result');
349+
347350
(function() {
348351
strictEqual(_.indexOf(arguments, NaN), 2, 'Expected arguments [1, 2, NaN] to contain NaN');
349352
}(1, 2, NaN, NaN));
@@ -418,6 +421,9 @@
418421
strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN), 3, 'Expected [1, 2, NaN] to contain NaN');
419422
strictEqual(_.lastIndexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, NaN] to contain NaN');
420423

424+
strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, 2), 2, 'fromIndex does not affect result');
425+
strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, -2), 2, 'fromIndex does not affect result');
426+
421427
(function() {
422428
strictEqual(_.lastIndexOf(arguments, NaN), 3, 'Expected arguments [1, 2, NaN] to contain NaN');
423429
}(1, 2, NaN, NaN));

underscore.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@
616616
return array[i] === item ? i : -1;
617617
}
618618
if (item !== item) {
619-
return _.findIndex(slice.call(array, i), _.isNaN);
619+
var index = _.findIndex(slice.call(array, i), _.isNaN);
620+
return index >= 0 ? index + i : -1;
620621
}
621622
for (; i < length; i++) if (array[i] === item) return i;
622623
return -1;

0 commit comments

Comments
 (0)