diff --git a/lodash.js b/lodash.js index a33e8abdc..3c2d971fa 100644 --- a/lodash.js +++ b/lodash.js @@ -30,8 +30,9 @@ /** Used as the TypeError message for "Functions" methods */ var FUNC_ERROR_TEXT = 'Expected a function'; - /** Used as a reference for the max length of an array */ - var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1; + /** Used as references for the max length and index of an array */ + var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1, + MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1; /** * Used as the maximum length of an array-like value. @@ -2285,13 +2286,14 @@ valIsUndef = typeof value == 'undefined'; while (low < high) { - var mid = (low + high) >>> 1, - computed = iteratee(array[mid]); + var mid = floor((low + high) / 2), + computed = iteratee(array[mid]), + isReflexive = computed === computed; if (valIsNaN) { - var setLow = computed === computed; + var setLow = isReflexive || retHighest; } else if (valIsUndef) { - setLow = computed === computed && typeof computed != 'undefined'; + setLow = isReflexive && (retHighest || typeof computed != 'undefined'); } else { setLow = retHighest ? (computed <= value) : (computed < value); } @@ -2301,7 +2303,7 @@ high = mid; } } - return high; + return nativeMin(high, MAX_ARRAY_INDEX); } /** diff --git a/test/test.js b/test/test.js index fb365b158..fa1bf1ff3 100644 --- a/test/test.js +++ b/test/test.js @@ -10056,24 +10056,24 @@ ], function(array) { deepEqual(_.sortBy(array), expected); strictEqual(func(expected, 3), 2); - strictEqual(func(expected, undefined), 3); - strictEqual(func(expected, NaN), isSortedIndex ? 4 : 5); + strictEqual(func(expected, undefined), isSortedIndex ? 3 : 4); + strictEqual(func(expected, NaN), isSortedIndex ? 4 : 6); }); }); - test('`_.' + methodName + '` should support arrays larger than `Math.pow(2, 31) - 1`', 1, function() { - var length = Math.pow(2, 32) - 1, - index = length - 1, - array = Array(length), + test('`_.' + methodName + '` should support arrays larger than `Math.pow(2, 31) - 1`', 2, function() { + var array = [0], + length = Math.pow(2, 32) - 1, steps = 0; + array.length = length; if (array.length == length) { - array[index] = index; - func(array, index, function() { steps++; }); + var actual = func(array, undefined, function() { steps++; }); strictEqual(steps, 33); + strictEqual(actual, isSortedIndex ? 0 : (length - 1)); } else { - skipTest(); + skipTest(2); } }); });