Set Methods for JavaScript
This is a proposal to add methods like union and intersection to JavaScript's built-in Set
class.
It is currently at stage 3.
Proposal
This would add the following methods:
Set.prototype.intersection(other)
Set.prototype.union(other)
Set.prototype.difference(other)
Set.prototype.symmetricDifference(other)
Set.prototype.isSubsetOf(other)
Set.prototype.isSupersetOf(other)
Set.prototype.isDisjointFrom(other)
These methods would all require their arguments to be a Set, or at least something which looks like a Set in terms of having a numeric size
property as well as keys
and has
methods.
See details.md for details of current decisions made in this proposal.
Rendered spec text is available here.
Implementations
This proposal is ready for engines to implement and ship. See this issue for current status.
There are two outstanding issues dealing with edge cases to be discussed at the July plenary, which may ultimately result in slightly tweaked behavior: #84 and #98. These are not expected to cause compat issues.
TC39 meeting notes
- May 2018
- January 2019
- March 2022 - discussion of how to extend built-ins in general
- July 2022
- September 2022
- November 2022
(Semi)relevant previous discussions
- proposal-rm-builtin-subclassing
- Map#map and Map#filter
- Map.prototype.map and Map.prototype.filter (spec) + Set
- Map: filter/map and more
- Original topic regarding this proposal
- Newer topic regarding this proposal
Comparison with other languages
See other languages document to get overview of Set
methods in other languages.
Naming
See naming bikeshedding document for details.
We decided to choose:
- Symmetric difference -
symmetricDifference
- Intersection -
intersection
- Union -
union
- Difference -
difference