ts-set-utils
This library provides a small set of utility functions to Typescript's and Javascript's Set
class. The ES6 Set
has a relatively small api surface and this library fills some common set use cases. Python built-in set
class is used as a api template. Find the package here
API
Union
Signature A ∪ B
function union<T> (...sets: Set<T>[]): Set<T>;
Performs a union on all sets provided. A union merge two or more sets together. Read more on wikipedia
![Set Union alt text](https://upload.wikimedia.org/wikipedia/commons/3/30/Venn0111.svg)
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([2, 3, 6])
console.log(union(set1, set2));
Union Pair
Signature A ∪ B
function unionPair<T1, T2> (a: Set<T1>, b: Set<T2>): Set<T1 | T2>;
Performs a union on two sets of different types. This is a utility method of typescript users to mix the set element types
Example
interface Pet {
pet: string
}
const set1 = new Set([1, 2, 3])
const set2 = new Set([{ pet: 'dog' }, { pet: 'cat' }])
console.log(union(set1, set2));
Intersection
Signature A ∩ B
function intersection<T> (...sets: Set<T>[]): Set<T>;
Performs a intersection on all sets provided. A intersection finds common elements between two or more sets. Read more on wikipedia
![Set Intersection alt text](https://upload.wikimedia.org/wikipedia/commons/9/99/Venn0001.svg)
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([2, 3, 4])
console.log(intersection(set1, set2));
Disjoint
Signature A ∩ B = ∅
function disjoint<T> (a: Set<T>, b: Set<T>): boolean;
Determine if two sets are disjoint. Two sets are disjoint when they share no elements. Read more on wikipedia
![Disjoint Sets alt text](https://upload.wikimedia.org/wikipedia/commons/d/df/Disjunkte_Mengen.svg)
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([2, 3, 4])
const set3 = new Set([10])
console.log(disjoint(set1, set2));
console.log(disjoint(set2, set3))
Subset
Signature B ⊆ A
function subset<T> (a: Set<T>, b: Set<T>): boolean;
Determine if set B
is a subset of set A
. A set B
is a subset of A
if all elements of B
are in set A
. Read more on wikipedia
![Relative Complement alt text](https://upload.wikimedia.org/wikipedia/commons/b/b0/Venn_A_subset_B.svg)
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])
const set3 = new Set([1, 2])
console.log(subset(set1, set2));
console.log(subset(set1, set3));
console.log(subset(set3, set1));
Set Equal
Signature B = A or B ⊆ A && B ⊇ A
Determine if set B
is equal to set A
. From the wikipedia definition "Set A
and B
are equal if and only if they have precisely the same element". Read more on wikipedia
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])
const set3 = new Set([1, 2])
console.log(setEqual(set1, set2));
console.log(setEqual(set2, set3));
console.log(setEqual(set1, set1));
Proper Subset
Signature B ⊂ A
function properSubset<T> (a: Set<T>, b: Set<T>): boolean;
Determine if set B
is a proper subset of set A
. A set B
is a proper subset of A
if all elements of B
are in set A
and A
is a strictly larger. Read more on wikipedia
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([1, 2])
console.log(properSubset(set1, set1));
console.log(subset(set1, set2));
Difference
Signature A \ B
function difference<T> (a: Set<T>, b: Set<T>): Set<T>;
Calculate the set difference between set A
and B
. A set difference calculates a base set A
with a set B
removed. More formally this would be called the relative complement. Read more on wikipedia
![Relative Complement alt text](https://upload.wikimedia.org/wikipedia/commons/5/5a/Venn0010.svg)
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])
console.log(difference(set1, set2));
Symmetric Difference
Signature A ∆ B
function symmetricDifference<T>(...set<T>[]): Set<T>;
Calculate the symmetric difference between a collection of sets. A symmetric difference of two sets produces a set where elements are members of one set but not the other. The behavior is more complex for more sets. Read more on wikipedia
![Symmetric Differnce alt text](https://upload.wikimedia.org/wikipedia/commons/4/46/Venn0110.svg)
Example
const set1 = new Set([1, 2, 3])
const set2 = new Set([3, 4, 5])
console.log(symmetricDifference(set1, set2));