Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/core/operations/SetDifference.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ class SetDifference extends Operation {
* @returns {Object[]}
*/
runSetDifference(a, b) {
const excluded = new Set(b);
const seen = new Set();

return a
.filter((item) => {
return b.indexOf(item) === -1;
if (excluded.has(item) || seen.has(item)) {
return false;
}
seen.add(item);
return true;
})
.join(this.itemDelimiter);
}
Expand Down
9 changes: 8 additions & 1 deletion src/core/operations/SetIntersection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ class SetIntersection extends Operation {
* @returns {Object[]}
*/
runIntersect(a, b) {
const included = new Set(b);
const seen = new Set();

return a
.filter((item) => {
return b.indexOf(item) > -1;
if (!included.has(item) || seen.has(item)) {
return false;
}
seen.add(item);
return true;
})
.join(this.itemDelimiter);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/operations/tests/SetDifference.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,26 @@ TestRegister.addTests([
},
],
},
{
name: "Set Difference: duplicates in first set are removed",
input: "red,red,blue\n\nblue",
expectedOutput: "red",
recipeConfig: [
{
op: "Set Difference",
args: ["\n\n", ","],
},
],
},
{
name: "Set Difference: duplicates in both sets",
input: "1 1 2 2 3\n\n2 2 3 3",
expectedOutput: "1",
recipeConfig: [
{
op: "Set Difference",
args: ["\n\n", " "],
},
],
},
]);
24 changes: 23 additions & 1 deletion tests/operations/tests/SetIntersection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,27 @@ TestRegister.addTests([
args: ["z", "-"],
},
],
}
},
{
name: "Set Intersection: duplicates in first set are removed",
input: "red,red,blue\n\nred,blue",
expectedOutput: "red,blue",
recipeConfig: [
{
op: "Set Intersection",
args: ["\n\n", ","],
},
],
},
{
name: "Set Intersection: duplicates in both sets",
input: "1 1 2 2 3\n\n2 2 3 3 4",
expectedOutput: "2 3",
recipeConfig: [
{
op: "Set Intersection",
args: ["\n\n", " "],
},
],
},
]);