fix Set Difference and Set Intersection preserve duplicates from first sample (#2286)

This commit is contained in:
Willi Ballenthin 2026-06-20 09:52:56 +02:00 committed by GitHub
parent 0fd6190b46
commit 5a2eeed066
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 3 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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", " "],
},
],
},
]);

View File

@ -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", " "],
},
],
},
]);