Skip to content

Commit

Permalink
Issue jashkenas#225, adding _.union, _.difference, to complement _.wi…
Browse files Browse the repository at this point in the history
…thout and _.intersection.
  • Loading branch information
jashkenas committed Jul 12, 2011
1 parent 942d631 commit 0ec859a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 14 additions & 4 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,24 @@ $(document).ready(function() {
equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
});

test("arrays: intersect", function() {
test("arrays: intersection", function() {
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
equals(_.intersect(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
equals(_(stooges).intersect(leaders).join(''), 'moe', 'can perform an OO-style intersection');
var result = (function(){ return _.intersect(arguments, leaders); })('moe', 'curly', 'larry');
equals(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
equals(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
var result = (function(){ return _.intersection(arguments, leaders); })('moe', 'curly', 'larry');
equals(result.join(''), 'moe', 'works on an arguments object');
});

test("arrays: union", function() {
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
equals(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
});

test("arrays: difference", function() {
var result = _.difference([1, 2, 3], [2, 30, 40]);
equals(result.join(' '), '1 3', 'takes the difference of two arrays');
});

test('arrays: zip', function() {
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
var stooges = _.zip(names, ages, leaders);
Expand Down
19 changes: 15 additions & 4 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@

// Return a version of the array that does not contain the specified value(s).
_.without = function(array) {
var values = slice.call(arguments, 1);
return _.filter(array, function(value){ return !_.include(values, value); });
return _.difference(array, slice.call(arguments, 1));
};

// Produce a duplicate-free version of the array. If the array has already
Expand All @@ -341,9 +340,15 @@
}, []);
};

// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
return _.uniq(_.flatten(arguments));
};

// Produce an array that contains every item shared between all the
// passed-in arrays.
_.intersect = function(array) {
// passed-in arrays. (Aliased as "intersect" for back-compat.)
_.intersection = _.intersect = function(array) {
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
Expand All @@ -352,6 +357,12 @@
});
};

// Take the difference between one array and another.
// Only the elements present in just the first array will remain.
_.difference = function(array, other) {
return _.filter(array, function(value){ return !_.include(other, value); });
};

// Zip together multiple lists into a single array -- elements that share
// an index go together.
_.zip = function() {
Expand Down

0 comments on commit 0ec859a

Please sign in to comment.