This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.ts
90 lines (80 loc) · 2.28 KB
/
index.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
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
80
81
82
83
84
85
86
87
88
89
90
import md5 from './utils/md5';
import * as parser from './parser';
const invalidInputMessage: string = 'invalid input';
export interface Tag {
multiline?: boolean;
open?: (state: State) => void;
readContent?: (state: State, symbolCode: number) => void;
close?: (state: State, options: ReadOptions) => void;
readToken(state: State): number;
}
export interface State {
pos: number;
statementIndex: number;
transactionIndex: number;
tag?: Tag;
tagContentStart?: number;
tagContentEnd?: number;
data: Uint8Array;
statements: Statement[];
}
export interface BalanceInfo {
isCredit: boolean;
date: string;
currency: string;
value: number;
}
export interface Transaction {
id: string;
code: string;
fundsCode: string;
isCredit: boolean;
isExpense: boolean;
currency: string;
description: string;
amount: number;
valueDate: string;
entryDate: string;
customerReference: string;
bankReference: string;
}
export interface Statement {
transactions: Transaction[];
referenceNumber?: string;
relatedReferenceNumber?: string;
accountId?: string;
number?: string;
openingBalance?: BalanceInfo;
closingBalance?: BalanceInfo;
closingAvailableBalance?: BalanceInfo;
forwardAvailableBalance?: BalanceInfo;
additionalInformation?: string;
}
export interface ReadOptions {
getTransactionId(transaction: Transaction, index: number): string;
}
export function read(input: ArrayBuffer | Buffer, options?: ReadOptions): Promise<Statement[]> {
let data: Uint8Array | Buffer;
if (typeof Buffer !== 'undefined' && input instanceof Buffer) {
data = input;
} else if (typeof ArrayBuffer !== 'undefined' && input instanceof ArrayBuffer) {
data = new Uint8Array(input);
} else {
return Promise.reject(new Error(invalidInputMessage));
}
return parser
.read(
data,
Object.assign(
{
getTransactionId(transaction: Transaction) {
return md5(JSON.stringify(transaction));
}
},
options
)
)
.catch(() => {
return Promise.reject(new Error(invalidInputMessage));
});
}