Debian Codegraph @

frontend/src/entries/amount.ts

raw
1 import type { FormatterContext } from "../format";
2 import type { Validator } from "../lib/validation";
3 import { number, object, string } from "../lib/validation";
4
5 /** An amount is a pair of number and currency. */
6 export class Amount {
7 constructor(
8 readonly number: number,
9 readonly currency: string,
10 ) {}
11
12 /** Render to a string. */
13 str($ctx: FormatterContext): string {
14 return $ctx.amount(this.number, this.currency);
15 }
16
17 private static raw_validator = object({ number, currency: string });
18
19 static validator: Validator<Amount> = (json) =>
20 Amount.raw_validator(json).map(
21 ({ number, currency }) => new Amount(number, currency),
22 );
23 }