Skip to content

Commit 832a138

Browse files
committed
[ts] refactor interpreter
1 parent 58b0db0 commit 832a138

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

Diff for: interpreter-pattern/interpreter-pattern.ts

+24-50
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,20 @@
1212
*/
1313

1414
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;
5118

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]);
5426
}
5527

56-
private getCapitalized(str: string): string {
28+
private capitalize(str: string): string {
5729
return str.charAt(0).toUpperCase() + str.slice(1);
5830
}
5931

@@ -77,9 +49,11 @@ class Bit extends Expression {
7749
bits(quantity: number): string {
7850
return String(quantity);
7951
}
52+
8053
nibbles(quantity: number): string {
8154
return String(Math.floor(quantity / 4));
8255
}
56+
8357
bytes(quantity: number): string {
8458
return String(Math.floor(quantity / 8));
8559
}
@@ -89,9 +63,11 @@ class Nibble extends Expression {
8963
bits(quantity: number): string {
9064
return String(quantity * 4);
9165
}
66+
9267
nibbles(quantity: number): string {
9368
return String(quantity);
9469
}
70+
9571
bytes(quantity: number): string {
9672
return String(Math.floor(quantity / 2));
9773
}
@@ -101,16 +77,19 @@ class Byte extends Expression {
10177
bits(quantity: number): string {
10278
return String(quantity * 8);
10379
}
80+
10481
nibbles(quantity: number): string {
10582
return String(quantity * 2);
10683
}
84+
10785
bytes(quantity: number): string {
10886
return String(quantity);
10987
}
11088
}
11189

11290
//---------------------------------------------
113-
const getClassFromInput = (input): Bit | Nibble | Byte | null => {
91+
92+
function getClassFromInput(input: string) {
11493
switch (input) {
11594
case "Bits":
11695
return new Bit();
@@ -119,17 +98,12 @@ const getClassFromInput = (input): Bit | Nibble | Byte | null => {
11998
case "Nibbles":
12099
return new Nibble();
121100
default:
122-
return null;
101+
throw new Error(`unknown input class '${input}'`);
123102
}
124-
};
103+
}
125104

126105
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);
132107

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

Comments
 (0)