@@ -14,7 +14,9 @@ const TYPES = {
1414const capitalize = str => `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
1515
1616const camelCase = name =>
17- name . replace ( / ( - | _ | \. | \s ) + [ a - z ] / g, letter => letter . toUpperCase ( ) . replace ( / [ ^ 0 - 9 a - z ] / gi, '' ) ) ;
17+ name . replace ( / ( - | _ | \. | \s ) + [ a - z ] / g, letter =>
18+ letter . toUpperCase ( ) . replace ( / [ ^ 0 - 9 a - z ] / gi, '' )
19+ ) ;
1820
1921const buildTypes = ( spec , options ) => {
2022 const { definitions } = spec ;
@@ -47,8 +49,9 @@ const buildTypes = (spec, options) => {
4749 const buildNextEnum = ( [ ID , options ] ) => {
4850 output . push ( `enum ${ ID } {` ) ;
4951 options . forEach ( option => {
50- const name = typeof option === 'string' ? capitalize ( camelCase ( option ) ) : option ;
51- output . push ( `${ name } : ${ option } ;` ) ;
52+ const name =
53+ typeof option === 'string' ? capitalize ( camelCase ( option ) ) : option ;
54+ output . push ( `${ name } = ${ JSON . stringify ( option ) } ,` ) ;
5255 } ) ;
5356 output . push ( '}' ) ;
5457 } ;
@@ -70,13 +73,20 @@ const buildTypes = (spec, options) => {
7073 const name = `${ camelCase ( key ) } ${ optional ? '?' : '' } ` ;
7174 const type = getType ( value ) ;
7275
76+ if ( typeof value . description === 'string' ) {
77+ // Print out descriptions as comments, but only if there’s something there (.*)
78+ output . push (
79+ `// ${ value . description . replace ( / \n $ / , '' ) . replace ( / \n / g, '\n// ' ) } `
80+ ) ;
81+ }
82+
7383 // If this is a nested object, let’s add it to the stack for later
7484 if ( type === 'object' ) {
7585 const newID = camelCase ( `${ ID } _${ key } ` ) ;
7686 queue . push ( [ newID , value ] ) ;
7787 output . push ( `${ name } : ${ newID } ;` ) ;
7888 return ;
79- } else if ( options . enum === true && Array . isArray ( value . enum ) ) {
89+ } else if ( Array . isArray ( value . enum ) ) {
8090 const newID = camelCase ( `${ ID } _${ key } ` ) ;
8191 enumQueue . push ( [ newID , value . enum ] ) ;
8292 output . push ( `${ name } : ${ newID } ;` ) ;
@@ -102,8 +112,6 @@ const buildTypes = (spec, options) => {
102112 buildNextInterface ( ) ;
103113 }
104114
105- // console.log(output.join('\n'));
106-
107115 return output . join ( '\n' ) ;
108116} ;
109117
0 commit comments