1- Map = class Map < TKey , TValue > {
1+ Map = class Map < K , V > {
22 public static [ Symbol . species ] = Map ;
33 public [ Symbol . toStringTag ] = "Map" ;
44
5- public size : number ;
5+ // Type of key is actually K
6+ private items : { [ key : string ] : V } = { } ;
7+ public size = 0 ;
68
7- private items : { [ key : string ] : TValue } ; // Type of key is actually TKey
9+ constructor ( entries ?: Iterable < readonly [ K , V ] > | Array < readonly [ K , V ] > ) {
10+ if ( entries === undefined ) return ;
811
9- constructor ( other ?: Iterable < readonly [ TKey , TValue ] > | Array < readonly [ TKey , TValue ] > ) {
10- this . items = { } ;
11- this . size = 0 ;
12-
13- if ( other === undefined ) return ;
14-
15- const iterable = other as Iterable < [ TKey , TValue ] > ;
12+ const iterable = entries as Iterable < [ K , V ] > ;
1613 if ( iterable [ Symbol . iterator ] ) {
1714 // Iterate manually because Map is compiled with ES5 which doesn't support Iterables in for...of
1815 const iterator = iterable [ Symbol . iterator ] ( ) ;
@@ -21,11 +18,11 @@ Map = class Map<TKey, TValue> {
2118 if ( result . done ) {
2219 break ;
2320 }
24- const value : [ TKey , TValue ] = result . value ; // Ensures index is offset when tuple is accessed
21+ const value : [ K , V ] = result . value ; // Ensures index is offset when tuple is accessed
2522 this . set ( value [ 0 ] , value [ 1 ] ) ;
2623 }
2724 } else {
28- const array = other as Array < [ TKey , TValue ] > ;
25+ const array = entries as Array < [ K , V ] > ;
2926 this . size = array . length ;
3027 for ( const kvp of array ) {
3128 this . items [ kvp [ 0 ] as any ] = kvp [ 1 ] ;
@@ -39,7 +36,7 @@ Map = class Map<TKey, TValue> {
3936 return ;
4037 }
4138
42- public delete ( key : TKey ) : boolean {
39+ public delete ( key : K ) : boolean {
4340 const contains = this . has ( key ) ;
4441 if ( contains ) {
4542 this . size -- ;
@@ -48,71 +45,71 @@ Map = class Map<TKey, TValue> {
4845 return contains ;
4946 }
5047
51- public [ Symbol . iterator ] ( ) : IterableIterator < [ TKey , TValue ] > {
48+ public forEach ( callback : ( value : V , key : K , map : Map < K , V > ) => any ) : void {
49+ for ( const key in this . items ) {
50+ callback ( this . items [ key ] , key as any , this ) ;
51+ }
52+ return ;
53+ }
54+
55+ public get ( key : K ) : V | undefined {
56+ return this . items [ key as any ] ;
57+ }
58+
59+ public has ( key : K ) : boolean {
60+ return this . items [ key as any ] !== undefined ;
61+ }
62+
63+ public set ( key : K , value : V ) : this {
64+ if ( ! this . has ( key ) ) {
65+ this . size ++ ;
66+ }
67+ this . items [ key as any ] = value ;
68+ return this ;
69+ }
70+
71+ public [ Symbol . iterator ] ( ) : IterableIterator < [ K , V ] > {
5272 return this . entries ( ) ;
5373 }
5474
55- public entries ( ) : IterableIterator < [ TKey , TValue ] > {
75+ public entries ( ) : IterableIterator < [ K , V ] > {
5676 const items = this . items ;
57- let key : TKey ;
58- let value : TValue ;
77+ let key : K ;
78+ let value : V ;
5979 return {
60- [ Symbol . iterator ] ( ) : IterableIterator < [ TKey , TValue ] > {
80+ [ Symbol . iterator ] ( ) : IterableIterator < [ K , V ] > {
6181 return this ;
6282 } ,
63- next ( ) : IteratorResult < [ TKey , TValue ] > {
83+ next ( ) : IteratorResult < [ K , V ] > {
6484 [ key , value ] = next ( items , key ) ;
6585 return { done : ! key , value : [ key , value ] } ;
6686 } ,
6787 } ;
6888 }
6989
70- public forEach ( callback : ( value : TValue , key : TKey , map : Map < TKey , TValue > ) => any ) : void {
71- for ( const key in this . items ) {
72- callback ( this . items [ key ] , key as any , this ) ;
73- }
74- return ;
75- }
76-
77- public get ( key : TKey ) : TValue | undefined {
78- return this . items [ key as any ] ;
79- }
80-
81- public has ( key : TKey ) : boolean {
82- return this . items [ key as any ] !== undefined ;
83- }
84-
85- public keys ( ) : IterableIterator < TKey > {
90+ public keys ( ) : IterableIterator < K > {
8691 const items = this . items ;
87- let key : TKey ;
92+ let key : K ;
8893 return {
89- [ Symbol . iterator ] ( ) : IterableIterator < TKey > {
94+ [ Symbol . iterator ] ( ) : IterableIterator < K > {
9095 return this ;
9196 } ,
92- next ( ) : IteratorResult < TKey > {
97+ next ( ) : IteratorResult < K > {
9398 [ key ] = next ( items , key ) ;
9499 return { done : ! key , value : key } ;
95100 } ,
96101 } ;
97102 }
98103
99- public set ( key : TKey , value : TValue ) : this {
100- if ( ! this . has ( key ) ) {
101- this . size ++ ;
102- }
103- this . items [ key as any ] = value ;
104- return this ;
105- }
106-
107- public values ( ) : IterableIterator < TValue > {
104+ public values ( ) : IterableIterator < V > {
108105 const items = this . items ;
109- let key : TKey ;
110- let value : TValue ;
106+ let key : K ;
107+ let value : V ;
111108 return {
112- [ Symbol . iterator ] ( ) : IterableIterator < TValue > {
109+ [ Symbol . iterator ] ( ) : IterableIterator < V > {
113110 return this ;
114111 } ,
115- next ( ) : IteratorResult < TValue > {
112+ next ( ) : IteratorResult < V > {
116113 [ key , value ] = next ( items , key ) ;
117114 return { done : ! key , value } ;
118115 } ,
0 commit comments