Last active
May 26, 2018 17:43
-
-
Save patrickroberts/4e6344a710569e4b7d280edc5684188d to your computer and use it in GitHub Desktop.
A Map whose values are Sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lookup extends Map { | |
get [Symbol.toStringTag] () { return 'Lookup' } | |
constructor (iterable = []) { | |
super( | |
[...iterable].map( | |
([key, value]) => [ | |
key, | |
value instanceof Set | |
? value | |
: new Set([value]) | |
] | |
) | |
) | |
} | |
add (key, value) { | |
if (this.has(key)) { | |
this.get(key).add(value) | |
} else { | |
this.set(key, value) | |
} | |
return this | |
} | |
get (key) { | |
return super.get(key) || new Set() | |
} | |
set (key, value) { | |
return super.set(key, new Set([value])) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment