Skip to content

Instantly share code, notes, and snippets.

@BrandonZacharie
Created July 19, 2016 22:48
Show Gist options
  • Save BrandonZacharie/f1f1ccd86ab0976e936616e2914171e3 to your computer and use it in GitHub Desktop.
Save BrandonZacharie/f1f1ccd86ab0976e936616e2914171e3 to your computer and use it in GitHub Desktop.
'use strict';
/**
* Provides extensions to the built-in `Set`.
* @class
*/
module.exports = class Set extends Set {
/**
* Returns a `Set` composed of the elements in the
* current `Set` and a given `set`.
* @argument {Set}
* @return {Set}
*/
union(set) {
return new Set([...this, ...set]);
}
/**
* Returns a `Set` whose elements are in either the
* current `Set` or a given `set`.
* @argument {Set}
* @return {Set}
*/
difference(set) {
return new Set([...this].filter((v) => !set.has(v)));
}
/**
* Returns a `Set` whose elements are in both the
* current `Set` and a given `set`.
* @argument {Set}
* @return {Set}
*/
intersection(set) {
return new Set([...this].filter((v) => set.has(v)));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment