File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -1977,7 +1977,15 @@ export class Serializer {
1977
1977
this . serializeRegistry . register ( ReflectionKind . enum , ( type , state ) => state . addSetter ( state . accessor ) ) ;
1978
1978
this . deserializeRegistry . register ( ReflectionKind . enum , ( type , state ) => {
1979
1979
const valuesVar = state . setVariable ( 'values' , type . values ) ;
1980
+ const lowercaseNames = state . setVariable ( 'lowercaseNames' , Object . keys ( type . enum ) . map ( v => v . toLowerCase ( ) ) ) ;
1981
+ const allLowercased = Object . keys ( type . enum ) . every ( v => v . toLowerCase ( ) === v ) ;
1982
+ const enumValues = state . setVariable ( 'enumValues' , type . values ) ;
1983
+ const allowLowercase = allLowercased ? '' : `
1984
+ ${ state . accessor } = ${ enumValues } [${ lowercaseNames } .indexOf(String(${ state . accessor } ).toLowerCase())] ?? ${ state . accessor } ;
1985
+ ` ;
1986
+
1980
1987
state . addCodeForSetter ( `
1988
+ ${ allowLowercase }
1981
1989
${ state . setter } = ${ state . accessor } ;
1982
1990
if (${ valuesVar } .indexOf(${ state . accessor } ) === -1) ${ state . throwCode ( 'enum' , `'No valid value of ' + ${ valuesVar } .join(', ')` ) }
1983
1991
` ) ;
Original file line number Diff line number Diff line change @@ -1019,6 +1019,22 @@ test('naming strategy camel case', () => {
1019
1019
}
1020
1020
} ) ;
1021
1021
1022
+ test ( 'enum mixed case' , ( ) => {
1023
+ enum Units {
1024
+ MILLIGRAM = 'm' ,
1025
+ GRAM = 'g' ,
1026
+ KILOGRAM = 'k' ,
1027
+ }
1028
+
1029
+ expect ( cast < Units > ( 'milligram' ) ) . toBe ( 'm' ) ;
1030
+ expect ( cast < Units > ( 'milligram' ) ) . toBe ( Units . MILLIGRAM ) ;
1031
+
1032
+ expect ( cast < Units > ( 'MilliGRAM' ) ) . toBe ( Units . MILLIGRAM ) ;
1033
+
1034
+ expect ( cast < Units > ( 'gram' ) ) . toBe ( 'g' ) ;
1035
+ expect ( cast < Units > ( 'gram' ) ) . toBe ( Units . GRAM ) ;
1036
+ } ) ;
1037
+
1022
1038
test ( 'enum union' , ( ) => {
1023
1039
enum StatEnginePowerUnit {
1024
1040
Hp = 'hp' ,
You can’t perform that action at this time.
0 commit comments