File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11export default class MultiMap < K , V > {
22 private map : Map < K , Set < V > > = new Map ( )
33
4+ constructor ( iterable ?: Iterable < [ K , V ] > ) {
5+ if ( iterable ) {
6+ for ( const [ k , v ] of iterable ) {
7+ this . set ( k , v )
8+ }
9+ }
10+ }
11+
412 get ( key : K ) : Set < V > {
513 const values = this . map . get ( key )
614 return values ? values : new Set ( )
Original file line number Diff line number Diff line change 11import MultiMap from '../dist/index.js'
22
33describe ( 'multimap' , function ( ) {
4+ it ( 'initializes from an iterable' , function ( ) {
5+ const map = new MultiMap ( [
6+ [ 'k1' , 1 ] ,
7+ [ 'k1' , 2 ] ,
8+ [ 'k2' , 3 ]
9+ ] )
10+ assert . deepEqual ( [ 1 , 2 ] , [ ...map . get ( 'k1' ) ] )
11+ assert . deepEqual ( [ 3 ] , [ ...map . get ( 'k2' ) ] )
12+ } )
13+
414 it ( 'size measures keys not values' , function ( ) {
515 const map = new MultiMap ( )
616 assert . equal ( 0 , map . size )
You can’t perform that action at this time.
0 commit comments