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:
parent
278421a1c8
commit
fbc58ca1fc
@ -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]);
|
||||||
|
});
|
@ -6,7 +6,7 @@
|
|||||||
// todo: replace the use of this methods with set.union and set.difference when
|
// todo: replace the use of this methods with set.union and set.difference when
|
||||||
// it's available.
|
// 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);
|
const result = new Set(a);
|
||||||
for (const element of b) {
|
for (const element of b) {
|
||||||
result.add(element);
|
result.add(element);
|
||||||
@ -14,8 +14,8 @@ export const union = <T>(a: Iterable<T>, b: Set<T>) => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const difference = <T>(a: Iterable<T>, b: Set<T>) => {
|
export const difference = <T>(a: Iterable<T>, b: Set<T>): Set<T> => {
|
||||||
const result = new Set(a);
|
const result = new Set<T>();
|
||||||
for (const element of a) {
|
for (const element of a) {
|
||||||
if (!b.has(element)) {
|
if (!b.has(element)) {
|
||||||
result.add(element);
|
result.add(element);
|
||||||
|
Loading…
Reference in New Issue
Block a user