SuperSet
A library for ES6 that extends the built-in Set
class to implement the missing fundamental methods such as map()
,
filter()
and reduce()
in addition to basic set methods such as union()
and isSubsetOf()
.
map(func, thisArg)
The map()
method creates a new set with the results of calling a provided function on every element in this set.
Parameters
func
Function that produces an element of the new set. Invoked with arguments (element, element, setObj)
.
thisArg
Optional. Value to use as this
when executing func
.
Example
Mapping a set of numbers to a set of square roots
let numbers = new SuperSet([1, 4, 9]);
numbers.map(Math.sqrt); // → SuperSet { 1, 2, 3 }
See Array.prototype.map on MDN for more examples.
filter(func, thisArg)
The filter()
method creates a new set with all elements that pass the test implemented by the provided function.
Parameters
func
Function to test each element of the set. Invoked with arguments (element, element, setObj)
. Return true
to keep the
element, false
otherwise.
thisArg
Optional. Value to use as this
when executing func
.
Example
Filtering out all small values
new SuperSet([12, 5, 8, 130, 44]).filter(elem => elem >= 10); // → SuperSet { 12, 130, 44 }
See Array.prototype.filter on MDN for more examples.
union()
The union()
method returns a new set comprised of the set on which it is called joined with the sets provided as the
arguments.
Example
Merging two sets
let first3 = new SuperSet(["a", "b", "c"]);
let from2to5 = new SuperSet(["b", "c", "d", "e"]);
first3.union(from2to5); // → SuperSet { "a", "b", "c", "d", "e" }
See Array.prototype.concat on MDN for more examples.
every(func, thisArg)
The every()
method tests whether all elements in the set pass the test implemented by the provided function.
Parameters
func
Function to test each element of the set. Invoked with arguments (element, element, setObj)
.
thisArg
Optional. Value to use as this
when executing func
.
Example
Testing size of all set elements
new SuperSet([12, 5, 8, 130, 44]).every(elem => elem >= 10); // → false
new SuperSet([12, 54, 18, 130, 44]).every(elem => elem >= 10); // → true
See Array.prototype.every on MDN for more examples.
find(func, thisArg)
The find()
method returns a value in the set, if an element in the set satisfies the provided testing function.
Otherwise undefined
is returned.
Parameters
func
Function to test each element of the set. Invoked with arguments (element, element, setObj)
.
thisArg
Optional. Value to use as this
when executing func
.
Example
Find a prime number in a set
function isPrime(element, _, setObj) {
let start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1)
return false;
}
return element > 1;
}
new SuperSet([4, 6, 8, 12]).find(isPrime); // → undefined
new SuperSet([4, 5, 8, 12]).find(isPrime); // → 5
See Array.prototype.find on MDN for more examples.
join(separator)
The join()
method joins all elements of a set into a string.
Parameters
separator
Optional. Specifies a string to separate each element of the set. The separator is converted to a string if necessary.
If omitted, the set elements are separated with a comma. If separator
is an empty string, all elements are joined
without any characters in between them.
Example
Joining a set four different ways
const events = new SuperSet(["Wind", "Rain", "Fire"]);
events.join(); // → "Wind,Rain,Fire"
events.join(", "); // → "Wind, Rain, Fire"
events.join(" + "); // → "Wind + Rain + Fire"
events.join(""); // → "WindRainFire"
See Array.prototype.join on MDN for more examples.
reduce(func, initialValue)
The reduce()
method applies a function against an accumulator and each value of the set (in no particular order) to
reduce it to a single value.
Parameters
func
Function to execute on each value in the set. Invoked with arguments (accumulator, element, element, setObj)
.
initialValue
Optional. Value to use as the first argument to the first call of the callback.
Example
Sum all the values of a set
new SuperSet([0, 1, 2, 3]).reduce((total, elem) => total + elem); // → 6
See Array.prototype.reduce on MDN for more examples.
some(func, thisArg)
The some()
method tests whether some element in the array passes the test implemented by the provided function.
Parameters
func
Function to test each element of the set. Invoked with arguments (element, element, setObj)
.
thisArg
Optional. Value to use as this
when executing func
.
Example
Testing if any element in set is larger than or equal to 10
new SuperSet([2, 5, 8, 1, 4]).some(elem => elem > 10); // → false
new SuperSet([12, 5, 8, 1, 4]).some(elem => elem > 10); // → true
See Array.prototype.some on MDN for more examples.
isSubsetOf(otherSetObj)
The isSubsetOf()
method determines whether a set is a subset of a certain set. Returns true
or false
as
appropriate.
Example
let nums = new SuperSet([0, 1, 2, 3, 4]);
let even = new SuperSet([0, 2, 4]);
even.isSubsetOf(nums); // → true
even.isSubsetOf(even); // → true
nums.isSubsetOf(even); // → false
equals(otherSetObj)
The equals()
method determines whether a set is a equal to a certain set. Returns true
or false
as appropriate.
Example
let set1 = new SuperSet([0, 1, 2]);
let set2 = new SuperSet([1, 2]);
set1.equals(set2); // → false
set2.equals(set1); // → false
set1.equals(set1); // → true
set2.add(0);
set1.equals(set2); // → true
set2.equals(set1); // → true
intersect(otherSetObj)
The intersect()
method returns the intersection of a set with another set.
Example
let nums = new SuperSet([3, 4, 5, 6]);
let primes = new SuperSet([2, 3, 5, 7]);
nums.intersect(primes); // → SuperSet { 3, 5 }
diff(otherSetObj)
The diff()
method returns the elements in a set that are not in the other set provided.
Example
let nums = new SuperSet([3, 4, 5, 6]);
let primes = new SuperSet([2, 3, 5, 7]);
nums.diff(primes); // → SuperSet { 4, 6 }
symmetricDiff(otherSetObj)
The symmetricDiff()
of two sets A and B is the set of elements which are in either of the sets A or B but not in both.
Example
let nums = new SuperSet([3, 4, 5, 6]);
let primes = new SuperSet([2, 3, 5, 7]);
nums.symmetricDiff(primes); // → SuperSet { 2, 4, 6, 7 }
update(iterable)
The update()
methods adds all the elements from the provided iterable to the set.
Example
let nums = new SuperSet([0, 1, 2]);
nums.update([2, 4, 6]); // → SuperSet { 0, 1, 2, 4, 6 }
discard(iterable)
The discard() method deletes the iterable elements from the data set and returns the updated elements.
Example
let nums = new SuperSet([1, 2, 3, 4]);
nums.discard([1, 3]); // → SuperSet { 2, 4 }
Parts of this documentation are adapted from MDN.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.