From 13022a6bb0f20da2abbcbba1aa414a4a1c9bbf25 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 5 Sep 2014 09:25:48 -0700 Subject: [PATCH] Realign `baseSortedIndex` with `_.sortBy`. --- lodash.js | 17 ++++++++++------- test/test.js | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lodash.js b/lodash.js index 95929800b..babd39513 100644 --- a/lodash.js +++ b/lodash.js @@ -2258,17 +2258,20 @@ high = array ? array.length : low; value = iteratee(value); - var hintNum = typeof value == 'number' || - (value != null && isFunction(value.valueOf) && typeof value.valueOf() == 'number'); + + var valIsNaN = value !== value, + valIsUndef = typeof value == 'undefined'; while (low < high) { var mid = (low + high) >>> 1, - computed = iteratee(array[mid]), - setLow = retHighest ? (computed <= value) : (computed < value); + computed = iteratee(array[mid]); - if (hintNum && typeof computed != 'undefined') { - computed = +computed; - setLow = computed != computed || setLow; + if (valIsNaN) { + var setLow = computed === computed; + } else if (valIsUndef) { + setLow = computed === computed && typeof computed != 'undefined'; + } else { + setLow = retHighest ? (computed <= value) : (computed < value); } if (setLow) { low = mid + 1; diff --git a/test/test.js b/test/test.js index e4f6bd3e6..2e10a08fe 100644 --- a/test/test.js +++ b/test/test.js @@ -9996,21 +9996,21 @@ }); test('`_.' + methodName + '` should align with `_.sortBy`', 8, function() { - var array = [NaN, 1, 2, {}, NaN, undefined]; - deepEqual(_.sortBy(array), array); - strictEqual(func(array, 3), 5); + var expected = [1, 2, {}, undefined, NaN, NaN]; - array = [1, 2, NaN, {}, NaN, undefined]; - deepEqual(_.sortBy(array), array); - strictEqual(func(array, 3), 5); + deepEqual(_.sortBy([NaN, 1, 2, {}, NaN, undefined]), expected); + strictEqual(func(expected, 3), 2); - array = [NaN, '1', '2', {}, NaN, undefined]; - deepEqual(_.sortBy(array), array); - strictEqual(func(array, '3'), 3); + deepEqual(_.sortBy([1, 2, NaN, {}, NaN, undefined]), expected); + strictEqual(func(expected, 3), 2); - array = ['1', '2', NaN, {}, NaN, undefined]; - deepEqual(_.sortBy(array), array); - strictEqual(func(array, '3'), 2); + expected = ['1', '2', {}, undefined, NaN, NaN]; + + deepEqual(_.sortBy([NaN, '1', '2', {}, NaN, undefined]), expected); + strictEqual(func(expected, '3'), 2); + + deepEqual(_.sortBy(['1', '2', NaN, {}, NaN, undefined]), expected); + strictEqual(func(expected, '3'), 2); }); test('`_.' + methodName + '` should support arrays larger than `Math.pow(2, 31) - 1`', 1, function() {