From 0570b0f580fd67e655a4af3cc6d7e075a732af30 Mon Sep 17 00:00:00 2001 From: dodonki1223 Date: Tue, 7 Dec 2021 08:56:16 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E3=80=90=E7=AC=AC2=E7=AB=A0=20?= =?UTF-8?q?=E6=98=8E=E7=99=BD=E3=81=AA=E5=AE=9F=E8=A3=85=E3=80=91Dollar?= =?UTF-8?q?=E3=81=AE=E5=89=AF=E4=BD=9C=E7=94=A8=E3=81=AE=E8=A7=A3=E6=B6=88?= =?UTF-8?q?=E3=82=92=E3=81=99=E3=82=8B=E2=91=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit イミュータブルを意識して Dollar オブジェクトを返す実装に変更する --- TODO.md | 2 +- src/__tests__/dollar.test.ts | 7 +++---- src/dollar.ts | 5 +---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index 7120148..81b4cb2 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,5 @@ - [ ] $5 + 10 CHF = $10(レートが 2:1 の場合) - [x] $5 * 2 = $10 - [ ] amount を private にする -- [ ] Dollar の副作用どうする? +- [x] Dollar の副作用どうする? - [ ] Money の丸め処理どうする? diff --git a/src/__tests__/dollar.test.ts b/src/__tests__/dollar.test.ts index 5eed2c4..64edee9 100644 --- a/src/__tests__/dollar.test.ts +++ b/src/__tests__/dollar.test.ts @@ -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); }); diff --git a/src/dollar.ts b/src/dollar.ts index 25d57b9..7bdfd5f 100644 --- a/src/dollar.ts +++ b/src/dollar.ts @@ -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) } }