Skip to content

Instantly share code, notes, and snippets.

@patrickroberts
Last active May 26, 2018 17:43
Show Gist options
  • Save patrickroberts/4e6344a710569e4b7d280edc5684188d to your computer and use it in GitHub Desktop.
Save patrickroberts/4e6344a710569e4b7d280edc5684188d to your computer and use it in GitHub Desktop.
A Map whose values are Sets
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