Skip to content

Commit f9c4eac

Browse files
committed
Improve TS examples
1 parent 5c74066 commit f9c4eac

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

TypeScript/1-traditional.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class BankAccount {
5454
class Bank {
5555
private commands: AccountCommand[] = [];
5656

57-
operation(account: BankAccount, amount: number): void {
58-
const Command = amount < 0 ? Withdraw : Income;
59-
const command = new Command(account, Math.abs(amount));
57+
operation(account: BankAccount, value: number): void {
58+
const Command = value < 0 ? Withdraw : Income;
59+
const command = new Command(account, Math.abs(value));
6060
command.execute();
6161
this.commands.push(command);
6262
}

TypeScript/2-js-way.ts

+20-26
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,53 @@ const addAccount = (name: string): Account => {
1111
return account;
1212
};
1313

14+
type OperationType = 'withdraw' | 'income';
15+
1416
interface Command {
15-
operation: string;
17+
operation: OperationType;
1618
account: string;
1719
amount: number;
1820
}
1921

20-
type OperationType = 'withdraw' | 'income';
22+
interface Operation {
23+
execute: (command: Command) => void;
24+
undo: (command: Command) => void;
25+
};
26+
27+
type Operations = Record<OperationType, Operation>;
2128

22-
const OPERATIONS: Record<OperationType, {
23-
execute: (command: Command) => void;
24-
undo: (command: Command) => void;
25-
}> = {
29+
const OPERATIONS: Operations = {
2630
withdraw: {
2731
execute: (command: Command): void => {
2832
const account = accounts.get(command.account);
29-
if (account) {
30-
account.balance -= command.amount;
31-
}
33+
if (account) account.balance -= command.amount;
3234
},
3335
undo: (command: Command): void => {
3436
const account = accounts.get(command.account);
35-
if (account) {
36-
account.balance += command.amount;
37-
}
37+
if (account) account.balance += command.amount;
3838
},
3939
},
4040
income: {
4141
execute: (command: Command): void => {
4242
const account = accounts.get(command.account);
43-
if (account) {
44-
account.balance += command.amount;
45-
}
43+
if (account) account.balance += command.amount;
4644
},
4745
undo: (command: Command): void => {
4846
const account = accounts.get(command.account);
49-
if (account) {
50-
account.balance -= command.amount;
51-
}
47+
if (account) account.balance -= command.amount;
5248
},
5349
},
5450
};
5551

5652
class Bank {
57-
private commands: Command[] = [];
53+
private commands: Array<Command> = [];
5854

59-
operation(account: Account, amount: number): void {
60-
const operation = amount < 0 ? 'withdraw' : 'income';
55+
operation(account: Account, value: number): void {
56+
const operation = value < 0 ? 'withdraw' : 'income';
6157
const { execute } = OPERATIONS[operation];
62-
const command: Command = {
63-
operation,
64-
account: account.name,
65-
amount: Math.abs(amount),
66-
};
58+
const amount = Math.abs(value);
59+
const { name } = account;
60+
const command: Command = { operation, account: name, amount };
6761
this.commands.push(command);
6862
execute(command);
6963
}

0 commit comments

Comments
 (0)