set-extended
This module's aim is to expand the already available functionalities of the Set Class in JS to cover most of the usual operators when using applied Set Theory, such as: Union, Intersection or Symetric Difference of sets, Power Set, Cartesian Product, and so on.
Installation
set-extended is available on npm package manager.
npm install set-extended
Usage
First, import the module. I like to call it SuperSet, as it gives super powers to the Set class.
const SuperSet = require('set-extended');
Evaluate if a set is a subset of a set:
MySet.subsetOf(A)
const A = SuperSet.of([1,2]);
const B = SuperSet.of([1,2,3,4,5]);
console.log(A.subsetOf(B))
console.log(B.subsetOf(A))
---
true
false
Union of sets:
SuperSet.union(...args)
const A = SuperSet.of([1,2,3]);
const B = SuperSet.of([3,4,5]);
const C = SuperSet.union(A,B);
console.log(C)
---
SuperSet [Set] {1, 2, 3, 4, 5}
Intersection of sets
SuperSet.intersection(...args)
const A = SuperSet.of([1,2,3]);
const B = SuperSet.of([3,4,5]);
const C = SuperSet.intersection(A,B);
console.log(C)
---
SuperSet [Set] {3}
Cartesian product of 2 sets
SuperSet.cartesianProduct(A,B)
const A = SuperSet.of([1,2]);
const B = SuperSet.of([1,2,3]);
const C = SuperSet.cartesianProduct(A,B);
console.log(C)
---
SuperSet [Set] { [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ] }
Difference of 2 sets
MySet.difference(A)
const A = SuperSet.of([1,2]);
const B = SuperSet.of([1,2,3,4]);
const C = B.difference(A);
console.log(C)
---
SuperSet [Set] { 3, 4 }
Symmetric Difference of 2 sets
MySet.symmetricDifference(A)
Remember: The Symmetric Difference of two sets is the set of elements which are in either of the sets and not in their intersection.
const A = SuperSet.of([1,2]);
const B = SuperSet.of([1,2,3,4]);
const C = A.symmetricDifference(B);
console.log(C)
---
SuperSet [Set] { 1, 2, 5, 6 }
Cardinal of a Set
MySet.cardinal
Remember: We name cardinal of a Set to it's size. Similar to .length
on an array
const A = SuperSet.of([1,2,3,4,5]);
console.log(A.cardinal)
---
5
Power Set of a Set
MySet.powerSet
Remember: The definition of the power set is the set of all the possibles subsets of a certain set: Power Set
const A = SuperSet.of([1,2]);
console.log(A.powerSet)
---
SuperSet [Set] {
SuperSet [Set] {},
SuperSet [Set] { 2 },
SuperSet [Set] { 1 },
SuperSet [Set] { 1, 2 }
}
Contributing
Pull requests are welcome.