File tree Expand file tree Collapse file tree 3 files changed +16
-4
lines changed Expand file tree Collapse file tree 3 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -127,6 +127,16 @@ describe('"map" type', () => {
127127 exec ( type , { a : 1 , b : 2 , c : 3 } ) ;
128128 } ) ;
129129
130+ test ( 'serializes empty map' , ( ) => {
131+ const type = s . Map ( s . num ) ;
132+ exec ( type , { } ) ;
133+ } ) ;
134+
135+ test ( 'serializes a map with a single key' , ( ) => {
136+ const type = s . Map ( s . num ) ;
137+ exec ( type , { '0' : 0 } ) ;
138+ } ) ;
139+
130140 test ( 'serializes a map in a map' , ( ) => {
131141 const type = s . Map ( s . Map ( s . bool ) ) ;
132142 exec ( type , { a : { b : true } } ) ;
Original file line number Diff line number Diff line change @@ -186,6 +186,8 @@ export class TypeBuilder {
186186 ) ,
187187 ) . options ( node ) ;
188188 }
189+ case 'map' :
190+ return this . Map ( this . import ( node . type ) , node ) ;
189191 case 'const' :
190192 return this . Const ( node . value ) . options ( node ) ;
191193 case 'or' :
Original file line number Diff line number Diff line change @@ -1935,13 +1935,13 @@ export class MapType<T extends Type> extends AbstractType<schema.MapSchema<Schem
19351935 const r = ctx . codegen . var ( value . use ( ) ) ;
19361936 const rKeys = ctx . codegen . var ( `Object.keys(${ r } )` ) ;
19371937 const rLength = ctx . codegen . var ( `${ rKeys } .length` ) ;
1938- const rKey = ctx . codegen . r ( ) ;
1939- const rValue = ctx . codegen . r ( ) ;
1938+ const rKey = ctx . codegen . var ( ) ;
19401939 ctx . codegen . if ( `${ rLength } ` , ( ) => {
1941- ctx . js ( `s += asString(${ rKeys } [i]) + ':';` ) ;
1940+ ctx . js ( `${ rKey } = ${ rKeys } [0];` ) ;
1941+ ctx . js ( `s += asString(${ rKey } ) + ':';` ) ;
19421942 this . type . codegenJsonTextEncoder ( ctx , new JsExpression ( ( ) => `${ r } [${ rKey } ]` ) ) ;
19431943 } ) ;
1944- ctx . js ( `for (var ${ rKey } , ${ rValue } , i = 1; i < ${ rLength } ; i++) {` ) ;
1944+ ctx . js ( `for (var i = 1; i < ${ rLength } ; i++) {` ) ;
19451945 ctx . js ( `${ rKey } = ${ rKeys } [i];` ) ;
19461946 ctx . js ( `s += ',' + asString(${ rKey } ) + ':';` ) ;
19471947 this . type . codegenJsonTextEncoder ( ctx , new JsExpression ( ( ) => `${ r } [${ rKey } ]` ) ) ;
You can’t perform that action at this time.
0 commit comments