Skip to content

Commit

Permalink
fix(domain): added toString() to Money.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aahoogendoorn committed Sep 21, 2021
1 parent 0e9bdaf commit 539f2d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/easy/src/domain/structs/Money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class Money extends Struct {
add = (amount: number): Money => money(this.currency, this.value + amount);
subtract = (amount: number): Money => money(this.currency, this.value - amount);
times = (n: number): Money => money(this.currency, this.value * n);

toString(): string {
return `${this.currency.code} ${this.value?.toFixed(this.currency.digits)}`;
}
}

export const money = (currency: Currency, value: number): Money => new Money({ currency: currency.id, value });
Expand Down
5 changes: 5 additions & 0 deletions packages/easy/test/domain/structs/Money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('Money', () => {
expect(m2.currency).toBe(m.currency);
expect(m2.value).toBe(168);
});

test('toString', () => {
expect(m).toMatchText("€ 42.00");
expect(money(Currency.GBP, 16)).toMatchText("£ 16.00");
});
});

describe('isMoney', () => {
Expand Down

0 comments on commit 539f2d6

Please sign in to comment.