-
Notifications
You must be signed in to change notification settings - Fork 0
/
index5.ts
41 lines (35 loc) · 1.47 KB
/
index5.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Digits, Size } from 'taghiro';
describe("use taghiro tagged types to add domain constraints", () => {
type CreditCardNumber = string & Digits & Size<16>
type AccountNumber = string & Digits & Size<8>
type SortCode = string & Digits & Size<6>
type CreditCardMethod = {
type: 'CREDITCARD'
cardNumber: CreditCardNumber
}
type ChequeMethod = {
type: 'CHEQUE'
account: AccountNumber
sortCode: SortCode
}
type PaymentMethod = CreditCardMethod | ChequeMethod
function refund(name: string, pm: PaymentMethod, amount: number) {
switch (pm.type) {
case 'CREDITCARD':
return `transferring ${amount} to ${name}, ${pm.cardNumber}`
case 'CHEQUE':
const sc = pm.sortCode;
const sortCode = `${sc.substring(0, 2)}-${sc.substring(2, 2)}-${sc.substring(4, 2)}`
return `writing cheque for ${amount} to ${name}, A/C: ${pm.account} Sort: ${sortCode}`
}
}
const cn = '1234567812345678'
const sc = '1234567'
const ac = '102030'
test("refund to credit card", () =>
expect(refund('Mike', { type: 'CREDITCARD', cardNumber: cn }, 100))
.toEqual('transferring 100 to Mike, 1234567812345678'))
test("refund to cheque", () =>
expect(refund('Mike', { type: 'CHEQUE', account: ac, sortCode: sc }, 100))
.toEqual('writing cheque for 100 to Mike, A/C: 1234567 Sort: 10-20-30'))
})