Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merging in #194, adding an iterator to _.uniq

  • Loading branch information...
commit 03b341d0635b86124413a631c40e57d3b350cbeb 2 parents 75b2195 + b930716
Jeremy Ashkenas authored August 03, 2011
4  index.html
@@ -592,12 +592,14 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
592 592
 </pre>
593 593
 
594 594
       <p id="uniq">
595  
-        <b class="header">uniq</b><code>_.uniq(array, [isSorted])</code>
  595
+        <b class="header">uniq</b><code>_.uniq(array, [isSorted], [iterator])</code>
596 596
         <span class="alias">Alias: <b>unique</b></span>
597 597
         <br />
598 598
         Produces a duplicate-free version of the <b>array</b>, using <i>===</i> to test
599 599
         object equality. If you know in advance that the <b>array</b> is sorted,
600 600
         passing <i>true</i> for <b>isSorted</b> will run a much faster algorithm.
  601
+        If you want to compute unique items based on a transformation, pass an
  602
+        <b>iterator</b> function.
601 603
       </p>
602 604
       <pre>
603 605
 _.uniq([1, 2, 1, 3, 1, 4]);
8  test/arrays.js
@@ -61,6 +61,14 @@ $(document).ready(function() {
61 61
     var list = [1, 1, 1, 2, 2, 3];
62 62
     equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
63 63
 
  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
+
64 72
     var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
65 73
     equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
66 74
   });
12  underscore.js
@@ -333,11 +333,17 @@
333 333
   // Produce a duplicate-free version of the array. If the array has already
334 334
   // been sorted, you have the option of using a faster algorithm.
335 335
   // Aliased as `unique`.
336  
-  _.uniq = _.unique = function(array, isSorted) {
337  
-    return _.reduce(array, function(memo, el, i) {
338  
-      if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo[memo.length] = el;
  336
+  _.uniq = _.unique = function(array, isSorted, iterator) {
  337
+    var initial = iterator ? _.map(array, iterator) : array;
  338
+    var result = [];
  339
+    _.reduce(initial, function(memo, el, i) {
  340
+      if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
  341
+        memo[memo.length] = el;
  342
+        result[result.length] = array[i];
  343
+      }
339 344
       return memo;
340 345
     }, []);
  346
+    return result;
341 347
   };
342 348
 
343 349
   // Produce an array that contains the union: each distinct element from all of

0 notes on commit 03b341d

Please sign in to comment.
Something went wrong with that request. Please try again.