Utilities / Functional methods for manipulating JavaScript sets.
- Add Values
- add(set?, value, ...) - add specific values to a set
- union(set, list|set|iterable) - combine two arrays
- common values
- intersection(set, list|set|iterable) - items in both of the lists
- Remove values
- remove(set, value, ...) - remove specific values from set and return set
- difference(set, list|set|iteratable) - remove set values from another
- unique
- new Set([ ...utils.difference(setA, setB), ...utils.difference(setB, setA)])
Note that the EcmaScript is catching up and union, difference, etc. will be supported soon.
However, this library removes a value and returns the set - which can be very helpful for functional programming.
Methods
(static) add(setTarget, val) → {set}
Mutably adds a value to a set, and then returns the set. (Allowing Chaining)
(If you wish to immutably, use ES6: {...setA, value1, value2, ...setB, etc...}
or use the union(set, list|set|iterable) command below)
Example
setA = new Set([1, 2, 3]);
utils.array.add(setA, 4, 5, 6); // Set([1, 2, 3, 4, 5, 6])
Parameters:
Name | Type | Description |
---|---|---|
setTarget |
set | set to add values to |
val |
any | value to add to the set |
Returns:
setTarget
- Type
- set
(static) difference(setTarget, iteratable) → {set}
Mutably removes all the values from one set in another
Example
setA = new Set([1, 2, 3, 4, 5, 6])
setB = new Set([4, 5, 6])
utils.set.difference(setA, setB) // Set([1, 2, 3])
Parameters:
Name | Type | Description |
---|---|---|
setTarget |
set | set to remove values from |
iteratable |
iteratable | iteratable that can be removed from the set. |
Returns:
setTarget
- Type
- set
(static) findItemsNotContained(superSet, iteratableWithAllValues) → {Set}
Immutably verifies the superset contains all items in iteratable, and returns the set of items not found in the superset.
Example
const possibeSuperSet = new Set([1,2,3,4,5,6]);
const subset = new Set([4,5,6,7]);
set.findItemsNotContained(possibleSuperSet, subset); // Set([7]);
Parameters:
Name | Type | Description |
---|---|---|
superSet |
set | set to check it contains all iteratable items |
iteratableWithAllValues |
iteratable | iteratable with all items |
Returns:
- set with items not in setToCheck (or an empty set if all contained)
- Type
- Set
(static) intersection(sourceA, sourceB, …rest) → {Set}
Immutably identify all items that are common in two sets of iterable items
Note: this works with Arrays and other things iteratable
Example
setA = new Set([1, 2, 3, 4]);
setB = new Set([3, 4, 5, 6]);
utils.set.intersection(setA, setB); // Set([3, 4])
// Note that you can use other iteratable things too
utils.set.intersection([1, 2, 3, 4], [3, 4, 5, 6]); // Set([3, 4])
setC = new Set([3, 4, 9, 10]);
utils.set.intersection(setA, setB, setC); // Set([3, 4]);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
sourceA |
Set | the set to check for common items |
|
sourceB |
Set | another set to check for common items |
|
rest |
iteratable |
<repeatable> |
additional iteratables to verify |
Returns:
- set of items that are in all sources
- Type
- Set
(static) remove(setTarget, val) → {set}
Mutably removes a value to a set, and then returns the set. (Allowing for chaining)
Example
setA = new Set([1, 2, 3, 4, 5])
utils.set.remove(setA, 4, 5); // Set([1, 2, 3])
Parameters:
Name | Type | Description |
---|---|---|
setTarget |
set | set to remove values from |
val |
any | value to remove from the set |
Returns:
setTarget
- Type
- set
(static) union(setTarget, iteratable, …rest) → {set}
Mutably Adds all the values from a target into a set. (Allowing Chaining)
(If you wish to union immutably, use ES6: {...setA, ...setB}
)
Note: this works with Arrays and other things iteratable
Example
setA = new Set([1, 2, 3]);
setB = new Set([4, 5, 6];
array.union(setA, setB) // Set([1, 2, 3, 4, 5, 6])
setA = new Set([1, 2, 3]);
listB = [4, 5, 6];
array.union(setA, listB) // Set([1, 2, 3, 4, 5, 6])
setC = new Set([7, 8, 9]);
array.union(setA, listB, setC);
// Set(1, 2, 3, 4, 5, 6, 7, 8, 9);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
setTarget |
set | set to add values to |
|
iteratable |
iteratable | iteratable that can be unioned into the set. |
|
rest |
iteratable |
<repeatable> |
additional iteratables to add to the union |
Returns:
new set that contains values from all sources
- Type
- set