Skip to content

Commit 5c74066

Browse files
committed
Improve JS examples
1 parent babdc9f commit 5c74066

File tree

6 files changed

+26
-34
lines changed

6 files changed

+26
-34
lines changed

JavaScript/1-execute.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class Bank {
3737
this.commands = [];
3838
}
3939

40-
operation(account, amount) {
41-
const Command = amount < 0 ? Withdraw : Income;
42-
const command = new Command(account, Math.abs(amount));
40+
operation(account, value) {
41+
const Command = value < 0 ? Withdraw : Income;
42+
const command = new Command(account, Math.abs(value));
4343
command.execute();
4444
this.commands.push(command);
4545
}

JavaScript/2-undo.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class Bank {
4747
this.commands = [];
4848
}
4949

50-
operation(account, amount) {
51-
const Command = amount < 0 ? Withdraw : Income;
52-
const command = new Command(account, Math.abs(amount));
50+
operation(account, value) {
51+
const Command = value < 0 ? Withdraw : Income;
52+
const command = new Command(account, Math.abs(value));
5353
command.execute();
5454
this.commands.push(command);
5555
}

JavaScript/3-anemic.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ class Bank {
2020
this.commands = [];
2121
}
2222

23-
operation(account, amount) {
24-
const operation = amount < 0 ? 'withdraw' : 'income';
25-
const command = new AccountCommand(
26-
operation,
27-
account.name,
28-
Math.abs(amount),
29-
);
23+
operation(account, value) {
24+
const operation = value < 0 ? 'withdraw' : 'income';
25+
const amount = Math.abs(value);
26+
const { name } = account;
27+
const command = new AccountCommand(operation, name, amount);
3028
this.commands.push(command);
3129
account.balance += amount;
3230
}

JavaScript/4-operations.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ class Bank {
3939
this.commands = [];
4040
}
4141

42-
operation(account, amount) {
43-
const operation = amount < 0 ? 'withdraw' : 'income';
42+
operation(account, value) {
43+
const operation = value < 0 ? 'withdraw' : 'income';
4444
const execute = OPERATIONS[operation];
45-
const command = new AccountCommand(
46-
operation,
47-
account.name,
48-
Math.abs(amount),
49-
);
45+
const amount = Math.abs(value);
46+
const { name } = account;
47+
const command = new AccountCommand(operation, name, amount);
5048
const check = OPERATIONS.allowed;
5149
const allowed = check(command);
5250
if (!allowed) {

JavaScript/5-undo.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ class Bank {
5050
this.commands = [];
5151
}
5252

53-
operation(account, amount) {
54-
const operation = amount < 0 ? 'withdraw' : 'income';
53+
operation(account, value) {
54+
const operation = value < 0 ? 'withdraw' : 'income';
5555
const { execute } = OPERATIONS[operation];
56-
const command = new AccountCommand(
57-
operation,
58-
account.name,
59-
Math.abs(amount),
60-
);
56+
const amount = Math.abs(value);
57+
const { name } = account;
58+
const command = new AccountCommand(operation, name, amount);
6159
this.commands.push(command);
6260
execute(command);
6361
}

JavaScript/6-js-way.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ class Bank {
3636
this.commands = [];
3737
}
3838

39-
operation(account, amount) {
40-
const operation = amount < 0 ? 'withdraw' : 'income';
39+
operation(account, value) {
40+
const operation = value < 0 ? 'withdraw' : 'income';
4141
const { execute } = OPERATIONS[operation];
42-
const command = {
43-
operation,
44-
account: account.name,
45-
amount: Math.abs(amount),
46-
};
42+
const amount = Math.abs(value);
43+
const { name } = account;
44+
const command = { operation, account: name, amount };
4745
this.commands.push(command);
4846
execute(command);
4947
}

0 commit comments

Comments
 (0)