Skip to content

Commit

Permalink
Juggle arguments in _.uniq to allow an argument signature of `array…
Browse files Browse the repository at this point in the history
…, iterator, context`

Issue 832: Juggle arguments in `_.uniq` to allow an argument signature
of `array, iterator, context`
  • Loading branch information
elias6 committed Nov 5, 2012
1 parent 2852062 commit d88ea4d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ $(document).ready(function() {
var iterator = function(value) { return value.name; };
equal(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');

equal(_.map(_.uniq(list, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator without specifying whether array is sorted');

var iterator = function(value) { return value +1; };
var list = [1, 2, 2, 3, 4, 4];
equal(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');
Expand Down
5 changes: 5 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,11 @@
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iterator, context) {
if (_.isFunction(isSorted)) {
context = iterator;
iterator = isSorted;
isSorted = false;
}
var initial = iterator ? _.map(array, iterator, context) : array;
var results = [];
var seen = [];
Expand Down

0 comments on commit d88ea4d

Please sign in to comment.