Skip to content

Commit

Permalink
♻️ 【第2章 明白な実装】Dollarの副作用の解消をする②
Browse files Browse the repository at this point in the history
イミュータブルを意識して Dollar オブジェクトを返す実装に変更する
  • Loading branch information
dodonki1223 committed Dec 7, 2021
1 parent 119ae23 commit 0570b0f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- [ ] $5 + 10 CHF = $10(レートが 2:1 の場合)
- [x] $5 * 2 = $10
- [ ] amount を private にする
- [ ] Dollar の副作用どうする?
- [x] Dollar の副作用どうする?
- [ ] Money の丸め処理どうする?
7 changes: 3 additions & 4 deletions src/__tests__/dollar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Dollar } from '../dollar';

test('times', () => {
const five = new Dollar(5);
five.times(2)
expect(five.amount).toBe(10);
five.times(3)
expect(five.amount).toBe(15);

expect(five.times(2).amount).toBe(10);
expect(five.times(3).amount).toBe(15);
});
5 changes: 1 addition & 4 deletions src/dollar.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
export class Dollar {
private tmpAmount: number

constructor(public amount: number) {
this.tmpAmount = amount
this.amount = amount
}

times(multiplier: number) {
this.amount = this.tmpAmount * multiplier
return new Dollar(this.amount * multiplier)
}
}

0 comments on commit 0570b0f

Please sign in to comment.