Skip to content

Commit

Permalink
fix set diff
Browse files Browse the repository at this point in the history
  • Loading branch information
AGawrys committed Dec 16, 2022
1 parent 3437f64 commit 5ac33c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/core/utils/src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function sortEntry(entry: mixed) {

export function setDifference<T>(a: Set<T>, b: Set<T>): Set<T> {
let difference = new Set();
for (let e of a) {
if (!b.has(e)) {
let both = new Set([...a, ...b]);
for (let e of both) {
if (!b.has(e) || !a.has(e)) {
difference.add(e);
}
}
Expand Down
14 changes: 13 additions & 1 deletion packages/core/utils/test/collection.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// @flow

import assert from 'assert';
import {objectSortedEntries, objectSortedEntriesDeep} from '../src/collection';
import {
objectSortedEntries,
objectSortedEntriesDeep,
setDifference,
} from '../src/collection';

describe('objectSortedEntries', () => {
it('returns a sorted list of key/value tuples', () => {
Expand Down Expand Up @@ -38,3 +42,11 @@ describe('objectSortedEntriesDeep', () => {
);
});
});
describe('setDifference', () => {
it('returns a setDifference of two sets of T type', () => {
assert.deepEqual(
setDifference(new Set([1, 2, 3]), new Set([3, 4, 5])),
new Set([1, 2, 4, 5]),
);
});
});

0 comments on commit 5ac33c9

Please sign in to comment.