12
12
*/
13
13
14
14
class ConversionContext {
15
- private conversionQuestion : string = "" ;
16
- private conversionResponse : string = "" ;
17
- private fromConversion : string = "" ;
18
- private toConversion : string = "" ;
19
- private partsOfQuestion : Array < string > ;
20
-
21
- private quantity : number ;
22
-
23
- public constructor ( input : string ) {
24
- this . conversionQuestion = input ;
25
- this . partsOfQuestion = input . split ( " " ) ;
26
- this . fromConversion = this . getCapitalized (
27
- this . sanitize ( this . partsOfQuestion [ 1 ] )
28
- ) ;
29
- this . toConversion = this . sanitize ( this . partsOfQuestion [ 3 ] ) . toLowerCase ( ) ;
30
- this . quantity = Number ( this . partsOfQuestion [ 0 ] ) ;
31
- this . conversionResponse = `${ this . partsOfQuestion [ 0 ] } ${
32
- this . partsOfQuestion [ 1 ]
33
- } equals `;
34
- }
35
-
36
- public getInput ( ) : string {
37
- return this . conversionQuestion ;
38
- }
39
-
40
- public getFromConversion ( ) : string {
41
- return this . fromConversion ;
42
- }
43
-
44
- public getToConversion ( ) : string {
45
- return this . toConversion ;
46
- }
47
-
48
- public getResponse ( ) : string {
49
- return this . conversionResponse ;
50
- }
15
+ readonly from : keyof Expression ;
16
+ readonly to : keyof Expression ;
17
+ readonly quantity : number ;
51
18
52
- public getQuantity ( ) : number {
53
- return this . quantity ;
19
+ public constructor ( question : string ) {
20
+ const components = question . split ( " " ) ;
21
+ this . from = this . capitalize (
22
+ this . sanitize ( components [ 1 ] )
23
+ ) as keyof Expression ;
24
+ this . to = this . sanitize ( components [ 3 ] ) as keyof Expression ;
25
+ this . quantity = Number ( components [ 0 ] ) ;
54
26
}
55
27
56
- private getCapitalized ( str : string ) : string {
28
+ private capitalize ( str : string ) : string {
57
29
return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
58
30
}
59
31
@@ -77,9 +49,11 @@ class Bit extends Expression {
77
49
bits ( quantity : number ) : string {
78
50
return String ( quantity ) ;
79
51
}
52
+
80
53
nibbles ( quantity : number ) : string {
81
54
return String ( Math . floor ( quantity / 4 ) ) ;
82
55
}
56
+
83
57
bytes ( quantity : number ) : string {
84
58
return String ( Math . floor ( quantity / 8 ) ) ;
85
59
}
@@ -89,9 +63,11 @@ class Nibble extends Expression {
89
63
bits ( quantity : number ) : string {
90
64
return String ( quantity * 4 ) ;
91
65
}
66
+
92
67
nibbles ( quantity : number ) : string {
93
68
return String ( quantity ) ;
94
69
}
70
+
95
71
bytes ( quantity : number ) : string {
96
72
return String ( Math . floor ( quantity / 2 ) ) ;
97
73
}
@@ -101,16 +77,19 @@ class Byte extends Expression {
101
77
bits ( quantity : number ) : string {
102
78
return String ( quantity * 8 ) ;
103
79
}
80
+
104
81
nibbles ( quantity : number ) : string {
105
82
return String ( quantity * 2 ) ;
106
83
}
84
+
107
85
bytes ( quantity : number ) : string {
108
86
return String ( quantity ) ;
109
87
}
110
88
}
111
89
112
90
//---------------------------------------------
113
- const getClassFromInput = ( input ) : Bit | Nibble | Byte | null => {
91
+
92
+ function getClassFromInput ( input : string ) {
114
93
switch ( input ) {
115
94
case "Bits" :
116
95
return new Bit ( ) ;
@@ -119,17 +98,12 @@ const getClassFromInput = (input): Bit | Nibble | Byte | null => {
119
98
case "Nibbles" :
120
99
return new Nibble ( ) ;
121
100
default :
122
- return null ;
101
+ throw new Error ( `unknown input class ' ${ input } '` ) ;
123
102
}
124
- } ;
103
+ }
125
104
126
105
const questionAsked = "8 bits to bytes" ;
127
- const question : ConversionContext = new ConversionContext ( questionAsked ) ;
128
-
129
- const fromConversion : string = question . getFromConversion ( ) ;
130
- const toConversion : string = question . getToConversion ( ) ;
131
- const quantity : number = question . getQuantity ( ) ;
106
+ const { from, to, quantity } = new ConversionContext ( questionAsked ) ;
132
107
133
- const convertFrom : Expression = getClassFromInput ( fromConversion ) ;
134
- const result = convertFrom [ toConversion ] ( quantity ) ;
135
- console . log ( `${ quantity } ${ fromConversion } is ${ result } ${ toConversion } ` ) ;
108
+ const result = getClassFromInput ( from ) [ to ] ( quantity ) ;
109
+ console . log ( `${ quantity } ${ from } is ${ result } ${ to } ` ) ;
0 commit comments