Un-golf _.uniq().

This commit is contained in:
Kit Cambridge
2012-02-21 10:35:55 -07:00
parent 0285f47f78
commit 8808f7d531

View File

@@ -371,9 +371,12 @@
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iterator) {
var initial = iterator ? _.map(array, iterator) : array;
var results = [];
_.reduce(iterator ? _.map(array, iterator) : array, function (memo, value, index) {
if (array.length < 3 || isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
// The `isSorted` flag is irrelevant if the array only contains two elements.
if (array.length < 3) isSorted = true;
_.reduce(initial, function (memo, value, index) {
if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
memo.push(value);
results.push(array[index]);
}