The SetExt class extends the built-in Set class by adding some additional methods for working with sets.
addRange(list)
: Adds elements from the passed list to theSet
.union(list)
: Returns a newSet
representing the union of the currentSet
and the given list.intersection(list)
: Returns a newSet
containing the intersection of the currentSet
and the passed list.difference(list)
: Returns a newSet
containing the difference between the currentSet
and the given list.symmetricDifference(list)
: Returns a newSet
representing the symmetric difference between the currentSet
and the given list.isSuperset(subset)
: Checks whether the currentSet
is a superset of the given subset.isSubset(superset)
: Checks whether the currentSet
is a subset of the passed superset.toString(separator)
: Returns a string representation of aSet
, concatenating elements through the specified separator.toArray()
: Converts aSet
to an array.toStruct({ base, step })
: Transforming theSet
according to your structure.base
- initial view of the desired structure,step
- function-instruction for forming your data structure. Takes four parameters:- structure in the state of the previous step
- current element of the
Set
- current element index
Set
size
step
must return the updated structure.
Example:
SetExt([1, 2, 3]).toStruct({
base: {},
step: (struct, elem) => {
struct[elem] = elem;
return struct;
}
}); // { 1: 1, 2: 2, 3: 3 }