1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-06-27 01:19:00 +02:00

fix difference calc and add tests (#9950)

Fixes a whoopsie in the difference function and adds tests at the same
time.
This commit is contained in:
Thomas Heartman 2025-05-09 17:47:52 +02:00 committed by GitHub
parent 278421a1c8
commit fbc58ca1fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 3 deletions

View File

@ -0,0 +1,15 @@
import { difference, union } from './set-functions';
test('union', () => {
const a = [1, 2];
const b = new Set([2, 3]);
const c = union(a, b);
expect([...c]).toEqual([1, 2, 3]);
});
test('difference', () => {
const a = [1, 2];
const b = new Set([2, 3]);
const c = difference(a, b);
expect([...c]).toEqual([1]);
});

View File

@ -6,7 +6,7 @@
// todo: replace the use of this methods with set.union and set.difference when
// it's available.
export const union = <T>(a: Iterable<T>, b: Set<T>) => {
export const union = <T>(a: Iterable<T>, b: Set<T>): Set<T> => {
const result = new Set(a);
for (const element of b) {
result.add(element);
@ -14,8 +14,8 @@ export const union = <T>(a: Iterable<T>, b: Set<T>) => {
return result;
};
export const difference = <T>(a: Iterable<T>, b: Set<T>) => {
const result = new Set(a);
export const difference = <T>(a: Iterable<T>, b: Set<T>): Set<T> => {
const result = new Set<T>();
for (const element of a) {
if (!b.has(element)) {
result.add(element);