Cleanup baseSortedUniq and add more uniq related tests.

This commit is contained in:
John-David Dalton
2015-08-28 08:53:05 -07:00
parent fcbe4620cd
commit cffeec4713
2 changed files with 82 additions and 87 deletions

View File

@@ -2621,29 +2621,33 @@
* @returns {Array} Returns the new duplicate free array.
*/
function baseSortedUniq(array, iteratee) {
var index = -1,
var length = array.length;
if (!length) {
return [];
}
var index = 0,
indexOf = getIndexOf(),
isCommon = indexOf === baseIndexOf,
length = array.length,
seen = isCommon ? undefined : [],
resIndex = -1,
result = [];
value = array[0],
computed = iteratee ? iteratee(value, 0, array) : value,
seen = isCommon ? computed : [computed],
resIndex = 0,
result = [value];
while (++index < length) {
var value = array[index],
computed = iteratee ? iteratee(value, index, array) : value;
value = array[index],
computed = iteratee ? iteratee(value, index, array) : value;
if (isCommon && value === value) {
if (seen !== computed || !index) {
seen = computed
result[++resIndex] = value;
}
} else {
if (!index || indexOf(seen, computed, 0) < 0) {
seen.push(computed);
if (isCommon) {
if ((value === value ? (seen !== computed) : (indexOf(seen, computed, 0) < 0))) {
seen = computed;
result[++resIndex] = value;
}
}
else if (indexOf(seen, computed, 0) < 0) {
seen[++resIndex] = computed;
result[resIndex] = value;
}
}
return result;
}

View File

@@ -14320,25 +14320,6 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.sortedUniqBy');
(function() {
var objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
test('should work with an `iteratee` argument', 1, function() {
var array = _.sortBy(objects, 'a'),
expected = [objects[2], objects[0], objects[1]];
var actual = _.sortedUniqBy(array, function(object) {
return object.a;
});
deepEqual(actual, expected);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.spread');
(function() {
@@ -15964,61 +15945,9 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.uniqBy');
(function() {
var objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
test('should work with an `iteratee` argument', 1, function() {
var expected = objects.slice(0, 3);
var actual = _.uniqBy(objects, function(object) {
return object.a;
});
deepEqual(actual, expected);
});
test('should provide the correct `iteratee` arguments', 1, function() {
var args;
_.uniqBy(objects, function() {
args || (args = slice.call(arguments));
});
deepEqual(args, [objects[0]]);
});
test('should work with a "_.property" style `iteratee`', 2, function() {
var actual = _.uniqBy(objects, 'a');
deepEqual(actual, objects.slice(0, 3));
var arrays = [[2], [3], [1], [2], [3], [1]];
actual = _.uniqBy(arrays, 0);
deepEqual(actual, arrays.slice(0, 3));
});
_.each({
'an array': [0, 'a'],
'an object': { '0': 'a' },
'a number': 0,
'a string': '0'
},
function(iteratee, key) {
test('should work with ' + key + ' for `iteratee`', 1, function() {
var actual = _.uniqBy([['a'], ['b'], ['a']], iteratee);
deepEqual(actual, [['a'], ['b']]);
});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('uniq methods');
_.each(['uniq', 'uniqBy'], function(methodName) {
_.each(['uniq', 'uniqBy', 'sortedUniq', 'sortedUniqBy'], function(methodName) {
var func = _[methodName],
objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
@@ -16119,6 +16048,68 @@
/*--------------------------------------------------------------------------*/
QUnit.module('uniqBy methods');
_.each(['uniqBy', 'sortedUniqBy'], function(methodName) {
var func = _[methodName],
isSortedUniqBy = methodName == 'sortedUniqBy',
objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }, { 'a': 2 }, { 'a': 3 }, { 'a': 1 }];
if (isSortedUniqBy) {
objects = _.sortBy(objects, 'a');
}
test('`_.' + methodName + '` should work with an `iteratee` argument', 1, function() {
var expected = isSortedUniqBy ? [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }] : objects.slice(0, 3);
var actual = func(objects, function(object) {
return object.a;
});
deepEqual(actual, expected);
});
test('`_.' + methodName + '` should provide the correct `iteratee` arguments', 1, function() {
var args;
func(objects, function() {
args || (args = slice.call(arguments));
});
deepEqual(args, [objects[0]]);
});
test('`_.' + methodName + '` should work with a "_.property" style `iteratee`', 2, function() {
var expected = isSortedUniqBy ? [{ 'a': 1 }, { 'a': 2 }, { 'a': 3 }] : objects.slice(0, 3),
actual = func(objects, 'a');
deepEqual(actual, expected);
var arrays = [[2], [3], [1], [2], [3], [1]];
if (isSortedUniqBy) {
arrays = _.sortBy(arrays, 0);
}
expected = isSortedUniqBy ? [[1], [2], [3]] : arrays.slice(0, 3);
actual = func(arrays, 0);
deepEqual(actual, expected);
});
_.each({
'an array': [0, 'a'],
'an object': { '0': 'a' },
'a number': 0,
'a string': '0'
},
function(iteratee, key) {
test('`_.' + methodName + '` should work with ' + key + ' for `iteratee`', 1, function() {
var actual = func([['a'], ['a'], ['b']], iteratee);
deepEqual(actual, [['a'], ['b']]);
});
});
});
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.uniqueId');
(function() {