From c94ce8c7456eac53f62499aab305b683c4514ca4 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 24 Feb 2023 20:37:53 -0600 Subject: [PATCH] Add type-safe set functions --- src/sets.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/sets.ts diff --git a/src/sets.ts b/src/sets.ts new file mode 100644 index 0000000..517c391 --- /dev/null +++ b/src/sets.ts @@ -0,0 +1,13 @@ +export const union = (a: Set, b: Set) => new Set([...a, ...b]); +export const intersection = (a: Set, b: Set) => + new Set([...a].filter((x) => b.has(x))); +export const difference = (a: Set, b: Set) => + new Set([...a].filter((x) => !b.has(x))); +export const symmetric = (a: Set, b: Set) => + union(difference(a, b), difference(b, a)); +export const isSubsetOf = (a: Set, b: Set) => + [...b].every((x) => a.has(x)); +export const isSupersetOf = (a: Set, b: Set) => + [...a].every((x) => b.has(x)); +export const isDisjointFrom = (a: Set, b: Set) => + !intersection(a, b).size;