From f016840c533b2aa394cb3df1df5ceeb564583a65 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 29 Aug 2015 09:10:29 -0700 Subject: [PATCH] Correct `baseSortedUniqBy`. --- lodash.js | 8 ++++---- test/test.js | 51 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lodash.js b/lodash.js index 611876bd6..92551f8ed 100644 --- a/lodash.js +++ b/lodash.js @@ -2629,17 +2629,17 @@ indexOf = getIndexOf(), isCommon = indexOf === baseIndexOf, value = array[0], - computed = iteratee ? iteratee(value, 0, array) : value, + computed = iteratee ? iteratee(value) : value, seen = isCommon ? computed : [computed], resIndex = 0, result = [value]; while (++index < length) { value = array[index], - computed = iteratee ? iteratee(value, index, array) : value; + computed = iteratee ? iteratee(value) : value; if (isCommon) { - if ((value === value ? (seen !== computed) : (indexOf(seen, computed, 0) < 0))) { + if ((seen === seen ? (seen !== computed) : (computed === computed))) { seen = computed; result[++resIndex] = value; } @@ -2693,7 +2693,7 @@ var value = array[index], computed = iteratee ? iteratee(value) : value; - if (isCommon && value === value) { + if (isCommon && computed === computed) { var seenIndex = seen.length; while (seenIndex--) { if (seen[seenIndex] === computed) { diff --git a/test/test.js b/test/test.js index da5fae981..f3b089b72 100644 --- a/test/test.js +++ b/test/test.js @@ -15949,13 +15949,18 @@ _.each(['uniq', 'uniqBy', 'sortedUniq', 'sortedUniqBy'], function(methodName) { var func = _[methodName], + isSorted = /^sorted/.test(methodName); objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }]; - test('`_.' + methodName + '` should return unique values of an unsorted array', 1, function() { - var array = [2, 3, 1, 2, 3, 1]; - deepEqual(func(array), [2, 3, 1]); - }); - + if (isSorted) { + objects = _.sortBy(objects, 'a'); + } + else { + test('`_.' + methodName + '` should return unique values of an unsorted array', 1, function() { + var array = [2, 3, 1, 2, 3, 1]; + deepEqual(func(array), [2, 3, 1]); + }); + } test('`_.' + methodName + '` should return unique values of a sorted array', 1, function() { var array = [1, 1, 2, 2, 3]; deepEqual(func(array), [1, 2, 3]); @@ -15966,16 +15971,18 @@ }); test('`_.' + methodName + '` should not treat `NaN` as unique', 1, function() { - deepEqual(func([1, NaN, 3, NaN]), [1, NaN, 3]); + deepEqual(func([1, 3, NaN, NaN]), [1, 3, NaN]); }); test('`_.' + methodName + '` should work with large arrays', 1, function() { var largeArray = [], - expected = [0, 'a', {}], + expected = [0, {}, 'a'], count = Math.ceil(LARGE_ARRAY_SIZE / expected.length); - _.times(count, function() { - push.apply(largeArray, expected); + _.each(expected, function(value) { + _.times(count, function() { + largeArray.push(value); + }); }); deepEqual(func(largeArray), expected); @@ -15983,11 +15990,13 @@ test('`_.' + methodName + '` should work with large arrays of boolean, `NaN`, and nullish values', 1, function() { var largeArray = [], - expected = [true, false, NaN, null, undefined], + expected = [false, true, null, undefined, NaN], count = Math.ceil(LARGE_ARRAY_SIZE / expected.length); - _.times(count, function() { - push.apply(largeArray, expected); + _.each(expected, function(value) { + _.times(count, function() { + largeArray.push(value); + }); }); deepEqual(func(largeArray), expected); @@ -16011,7 +16020,7 @@ if (Symbol) { var expected = [ Symbol.hasInstance, Symbol.isConcatSpreadable, Symbol.iterator, - Symbol.match, Symbol.replace, Symbol.search, Symbol.species, + Symbol.match, Symbol.replace, Symbol.search, Symbol.species, Symbol.split, Symbol.toPrimitive, Symbol.toStringTag, Symbol.unscopables ]; @@ -16022,8 +16031,10 @@ return symbol || {}; }); - _.times(count, function() { - push.apply(largeArray, expected); + _.each(expected, function(value) { + _.times(count, function() { + largeArray.push(value); + }); }); deepEqual(func(largeArray), expected); @@ -16034,15 +16045,17 @@ }); test('`_.' + methodName + '` should distinguish between numbers and numeric strings', 1, function() { - var array = [], + var largeArray = [], expected = ['2', 2, Object('2'), Object(2)], count = Math.ceil(LARGE_ARRAY_SIZE / expected.length); - _.times(count, function() { - push.apply(array, expected); + _.each(expected, function(value) { + _.times(count, function() { + largeArray.push(value); + }); }); - deepEqual(func(array), expected); + deepEqual(func(largeArray), expected); }); });