statement-parser-fab
Version:
Parse bank and credit card statements. Updated fork with FAB (First Abu Dhabi Bank) support and maintained dependencies.
58 lines (57 loc) • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.exampleStatementParser = void 0;
const augment_vir_1 = require("augment-vir");
const statement_parser_1 = require("../statement-parser");
var State;
(function (State) {
State["Header"] = "header";
State["InnerState"] = "inner-state";
State["End"] = "end";
})(State || (State = {}));
exports.exampleStatementParser = (0, statement_parser_1.createStatementParser)({
action: performStateAction,
next: nextState,
initialState: State.Header,
endState: State.End,
parserKeywords: [],
});
const validPaymentRegExp = /(\d{2}\/\d{2})\s+(.+)\$([-,.\d]+)/;
function readPayment(line) {
const [, dateString, descriptionString, amountString,] = (0, augment_vir_1.safeMatch)(line, validPaymentRegExp);
if (dateString && descriptionString && amountString) {
return {
amount: Number((0, augment_vir_1.removeCommasFromNumberString)(amountString)),
description: descriptionString,
date: (0, augment_vir_1.createDateFromUtcIsoFormat)(dateString),
originalText: [line],
};
}
else {
return undefined;
}
}
function performStateAction(currentState, line, output) {
if (currentState === State.InnerState && line.match(validPaymentRegExp)) {
const transaction = readPayment(line);
if (transaction) {
output.incomes.push(transaction);
}
}
return output;
}
function nextState(currentState, line) {
line = line.toLowerCase();
switch (currentState) {
case State.Header:
return State.InnerState;
case State.InnerState:
if (line === 'end inner state') {
return State.End;
}
break;
case State.End:
break;
}
return currentState;
}