Skip to content

Commit b930716

Browse files
committed
Add optional iterator to _.uniq
1 parent c174663 commit b930716

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,20 @@ <h2>Array Functions</h2>
547547
</pre>
548548

549549
<p id="uniq">
550-
<b class="header">uniq</b><code>_.uniq(array, [isSorted])</code>
550+
<b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
551551
<span class="alias">Alias: <b>unique</b></span>
552552
<br />
553553
Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
554554
object equality. If you know in advance that the <b>array</b> is sorted,
555555
passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
556+
Can receive an iterator to determine which part of the element gets tested.
556557
</p>
557558
<pre>
558559
_.uniq([1, 2, 1, 3, 1, 4]);
559560
=&gt; [1, 2, 3, 4]
561+
562+
_.uniq([{name:'moe'}, {name:'curly'}, {name:'larry'}, {name:'curly'}], false, function (value) { return value.name; })
563+
=&gt;[{name:'moe'}, {name:'curly'}, {name:'larry'}]
560564
</pre>
561565

562566
<p id="intersect">

test/arrays.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ $(document).ready(function() {
6161
var list = [1, 1, 1, 2, 2, 3];
6262
equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
6363

64+
var list = [{name:'moe'}, {name:'curly'}, {name:'larry'}, {name:'curly'}];
65+
var iterator = function(value) { return value.name; };
66+
equals(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');
67+
68+
var iterator = function(value) { return value +1; };
69+
var list = [1, 2, 2, 3, 4, 4];
70+
equals(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');
71+
6472
var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
6573
equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
6674
});

underscore.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,17 @@
323323
// Produce a duplicate-free version of the array. If the array has already
324324
// been sorted, you have the option of using a faster algorithm.
325325
// Aliased as `unique`.
326-
_.uniq = _.unique = function(array, isSorted) {
327-
return _.reduce(array, function(memo, el, i) {
328-
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
326+
_.uniq = _.unique = function(array, isSorted, iterator) {
327+
var initial = iterator ? _.map(array, iterator) : array;
328+
var result = [];
329+
_.reduce(initial, function(memo, el, i) {
330+
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
331+
memo[memo.length] = el;
332+
result[result.length] = array[i];
333+
}
329334
return memo;
330335
}, []);
336+
return result;
331337
};
332338

333339
// Produce an array that contains every item shared between all the

0 commit comments

Comments
 (0)