-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoice-patterns.ts
More file actions
79 lines (69 loc) · 2.32 KB
/
Copy pathinvoice-patterns.ts
File metadata and controls
79 lines (69 loc) · 2.32 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class InvoicePatterns {
expense: string
datePattern: RegExp[]
accountPattern: RegExp[]
invoicePattern: RegExp[]
netAmountPattern: RegExp[]
gstAmountPattern: RegExp[]
totalAmountPattern: RegExp[]
constructor(
expense: string,
datePattern: string,
accountPattern: string,
totalAmountPattern: string,
netAmountPattern?: string,
gstAmountPattern?: string,
invoicePattern?: string) {
this.expense = expense
this.datePattern = [new RegExp(datePattern)]
this.accountPattern = [new RegExp(accountPattern)]
this.totalAmountPattern = [new RegExp(totalAmountPattern)]
this.netAmountPattern = []
if (netAmountPattern) {
this.netAmountPattern.push(new RegExp(netAmountPattern))
}
this.gstAmountPattern = []
if (gstAmountPattern) {
this.gstAmountPattern.push(new RegExp(gstAmountPattern))
}
this.invoicePattern = []
if (invoicePattern) {
this.invoicePattern.push(new RegExp(invoicePattern))
}
}
combine(patterns: InvoicePatterns): void {
this.datePattern = this.datePattern.concat(patterns.datePattern)
this.accountPattern = this.accountPattern.concat(patterns.accountPattern)
this.totalAmountPattern = this.totalAmountPattern.concat(patterns.totalAmountPattern)
this.netAmountPattern = this.netAmountPattern.concat(patterns.netAmountPattern)
this.gstAmountPattern = this.gstAmountPattern.concat(patterns.gstAmountPattern)
this.invoicePattern = this.invoicePattern.concat(patterns.invoicePattern)
}
matchDate(text: string): null | string {
return this.match(text, this.datePattern)
}
matchAccount(text: string): null | string {
return this.match(text, this.accountPattern)
}
matchTotalAmount(text: string): null | string {
return this.match(text, this.totalAmountPattern)
}
matchNetAmountPattern(text: string): null | string {
return this.match(text, this.netAmountPattern)
}
matchGstAmountPattern(text: string): null | string {
return this.match(text, this.gstAmountPattern)
}
matchInvoicePattern(text: string): null | string {
return this.match(text, this.invoicePattern)
}
match(text: string, patterns: RegExp[]): null | string {
for (const pattern in patterns) {
const match = text.match(pattern)
if (match) {
return match[1]
}
}
return null
}
}