File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed
Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change 11import { Enum } from "../src/index" ;
22
33describe ( "Enum" , ( ) => {
4- it ( "should return an object with values equal to keys" , ( ) => {
4+ it ( "returns an object with values equal to keys given multiple strings " , ( ) => {
55 expect ( Enum ( "BLACK" , "WHITE" ) ) . toEqual ( { BLACK : "BLACK" , WHITE : "WHITE" } ) ;
66 } ) ;
7+
8+ it ( "returns an object with values equal to keys given a single string" , ( ) => {
9+ expect ( Enum ( "BLACK" ) ) . toEqual ( { BLACK : "BLACK" } ) ;
10+ } ) ;
11+
12+ it ( "returns the original object given an object" , ( ) => {
13+ expect ( Enum ( {
14+ BLACK : "black" ,
15+ WHITE : "white" ,
16+ } ) ) . toEqual ( { BLACK : "black" , WHITE : "white" } ) ;
17+ } ) ;
718} ) ;
Original file line number Diff line number Diff line change 1- export function Enum < V extends string > ( ...values : V [ ] ) : { [ K in V ] : K } {
2- const result : any = { } ;
3- values . forEach ( ( value ) => result [ value ] = value ) ;
4- return result ;
1+ export function Enum < V extends string > ( ...values : V [ ] ) : { [ K in V ] : K } ;
2+ export function Enum <
3+ T extends { [ _ : string ] : V } ,
4+ V extends string
5+ > ( definition : T ) : T ;
6+ export function Enum ( ...values : any [ ] ) : any {
7+ if ( typeof values [ 0 ] === "string" ) {
8+ const result : any = { } ;
9+ for ( const value of values ) {
10+ result [ value ] = value ;
11+ }
12+ return result ;
13+ } else {
14+ return values [ 0 ] ;
15+ }
516}
617
718export type Enum < T > = T [ keyof T ] ;
You can’t perform that action at this time.
0 commit comments