Correct baseSortedUniqBy.

This commit is contained in:
John-David Dalton
2015-08-29 09:10:29 -07:00
parent cffeec4713
commit f016840c53
2 changed files with 36 additions and 23 deletions

View File

@@ -2629,17 +2629,17 @@
indexOf = getIndexOf(), indexOf = getIndexOf(),
isCommon = indexOf === baseIndexOf, isCommon = indexOf === baseIndexOf,
value = array[0], value = array[0],
computed = iteratee ? iteratee(value, 0, array) : value, computed = iteratee ? iteratee(value) : value,
seen = isCommon ? computed : [computed], seen = isCommon ? computed : [computed],
resIndex = 0, resIndex = 0,
result = [value]; result = [value];
while (++index < length) { while (++index < length) {
value = array[index], value = array[index],
computed = iteratee ? iteratee(value, index, array) : value; computed = iteratee ? iteratee(value) : value;
if (isCommon) { if (isCommon) {
if ((value === value ? (seen !== computed) : (indexOf(seen, computed, 0) < 0))) { if ((seen === seen ? (seen !== computed) : (computed === computed))) {
seen = computed; seen = computed;
result[++resIndex] = value; result[++resIndex] = value;
} }
@@ -2693,7 +2693,7 @@
var value = array[index], var value = array[index],
computed = iteratee ? iteratee(value) : value; computed = iteratee ? iteratee(value) : value;
if (isCommon && value === value) { if (isCommon && computed === computed) {
var seenIndex = seen.length; var seenIndex = seen.length;
while (seenIndex--) { while (seenIndex--) {
if (seen[seenIndex] === computed) { if (seen[seenIndex] === computed) {

View File

@@ -15949,13 +15949,18 @@
_.each(['uniq', 'uniqBy', 'sortedUniq', 'sortedUniqBy'], function(methodName) { _.each(['uniq', 'uniqBy', 'sortedUniq', 'sortedUniqBy'], function(methodName) {
var func = _[methodName], var func = _[methodName],
isSorted = /^sorted/.test(methodName);
objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }]; 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() { if (isSorted) {
var array = [2, 3, 1, 2, 3, 1]; objects = _.sortBy(objects, 'a');
deepEqual(func(array), [2, 3, 1]); }
}); 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() { test('`_.' + methodName + '` should return unique values of a sorted array', 1, function() {
var array = [1, 1, 2, 2, 3]; var array = [1, 1, 2, 2, 3];
deepEqual(func(array), [1, 2, 3]); deepEqual(func(array), [1, 2, 3]);
@@ -15966,16 +15971,18 @@
}); });
test('`_.' + methodName + '` should not treat `NaN` as unique', 1, function() { 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() { test('`_.' + methodName + '` should work with large arrays', 1, function() {
var largeArray = [], var largeArray = [],
expected = [0, 'a', {}], expected = [0, {}, 'a'],
count = Math.ceil(LARGE_ARRAY_SIZE / expected.length); count = Math.ceil(LARGE_ARRAY_SIZE / expected.length);
_.times(count, function() { _.each(expected, function(value) {
push.apply(largeArray, expected); _.times(count, function() {
largeArray.push(value);
});
}); });
deepEqual(func(largeArray), expected); deepEqual(func(largeArray), expected);
@@ -15983,11 +15990,13 @@
test('`_.' + methodName + '` should work with large arrays of boolean, `NaN`, and nullish values', 1, function() { test('`_.' + methodName + '` should work with large arrays of boolean, `NaN`, and nullish values', 1, function() {
var largeArray = [], var largeArray = [],
expected = [true, false, NaN, null, undefined], expected = [false, true, null, undefined, NaN],
count = Math.ceil(LARGE_ARRAY_SIZE / expected.length); count = Math.ceil(LARGE_ARRAY_SIZE / expected.length);
_.times(count, function() { _.each(expected, function(value) {
push.apply(largeArray, expected); _.times(count, function() {
largeArray.push(value);
});
}); });
deepEqual(func(largeArray), expected); deepEqual(func(largeArray), expected);
@@ -16011,7 +16020,7 @@
if (Symbol) { if (Symbol) {
var expected = [ var expected = [
Symbol.hasInstance, Symbol.isConcatSpreadable, Symbol.iterator, 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 Symbol.split, Symbol.toPrimitive, Symbol.toStringTag, Symbol.unscopables
]; ];
@@ -16022,8 +16031,10 @@
return symbol || {}; return symbol || {};
}); });
_.times(count, function() { _.each(expected, function(value) {
push.apply(largeArray, expected); _.times(count, function() {
largeArray.push(value);
});
}); });
deepEqual(func(largeArray), expected); deepEqual(func(largeArray), expected);
@@ -16034,15 +16045,17 @@
}); });
test('`_.' + methodName + '` should distinguish between numbers and numeric strings', 1, function() { test('`_.' + methodName + '` should distinguish between numbers and numeric strings', 1, function() {
var array = [], var largeArray = [],
expected = ['2', 2, Object('2'), Object(2)], expected = ['2', 2, Object('2'), Object(2)],
count = Math.ceil(LARGE_ARRAY_SIZE / expected.length); count = Math.ceil(LARGE_ARRAY_SIZE / expected.length);
_.times(count, function() { _.each(expected, function(value) {
push.apply(array, expected); _.times(count, function() {
largeArray.push(value);
});
}); });
deepEqual(func(array), expected); deepEqual(func(largeArray), expected);
}); });
}); });