Last active
June 21, 2018 02:44
-
-
Save davidchase/59b1458878f357f90ca99d01dea10a42 to your computer and use it in GitHub Desktop.
Object+Array Simple Multi-Map
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 MultiMap { | |
constructor(container = Array) { | |
this.map = {}; | |
this.Container = container; | |
} | |
set(k, v) { | |
let container = this.get(k); | |
if (!container) { | |
container = new this.Container(); | |
this.map[k] = container.concat(v) | |
} | |
container.push(v) | |
return this; | |
} | |
get(k) { | |
return this.map[k]; | |
} | |
has(k) { | |
return this.map.hasOwnProperty(k); | |
} | |
values() { | |
return Object.values(this.map); | |
} | |
keys() { | |
return Object.keys(this.map); | |
} | |
entries() { | |
return Object.entries(this.map); | |
} | |
inspect() { | |
return this.map; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment