UNPKG

statement-parser-fab

Version:

Parse bank and credit card statements. Updated fork with FAB (First Abu Dhabi Bank) support and maintained dependencies.

63 lines (62 loc) 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createStatementParser = exports.createStatementParserInputDefault = void 0; const augment_vir_1 = require("augment-vir"); const read_pdf_1 = require("../pdf/read-pdf"); const parser_state_machine_1 = require("./parser-state-machine"); exports.createStatementParserInputDefault = { async pdfProcessing(filePath) { return await (0, read_pdf_1.readPdf)(filePath); }, }; function createStatementParser(rawInputs) { const inputs = { ...exports.createStatementParserInputDefault, ...rawInputs, }; const pdfProcessing = inputs.pdfProcessing; if (!pdfProcessing) { throw new Error('Missing pdf processing method'); } const parseText = ({ textLines, parserOptions: inputParserOptions, debug, name, }) => { const stateMachineInputs = { // ParserInitInput is a subtype of inputs' type ...inputs, name, debug, parserOptions: inputParserOptions, }; const runStateMachine = (0, parser_state_machine_1.createParserStateMachine)(stateMachineInputs); const output = runStateMachine(textLines); if (inputs.outputValidation) { inputs.outputValidation(output); } return output; }; const convertPdfToText = async (filePath) => { const pdfPages = await pdfProcessing(filePath); const textLines = (0, augment_vir_1.flatten2dArray)(pdfPages); return textLines; }; const parsePdf = async ({ filePath, parserOptions: inputParserOptions, debug, }) => { const textLines = await convertPdfToText(filePath); return parseText({ parserOptions: inputParserOptions, debug, name: filePath, textLines, }); }; const defaultParserOptionsWrapper = inputs.defaultParserOptions ? { defaultParserOptions: inputs.defaultParserOptions } : {}; const returnValue = { parsePdf, parseText, convertPdfToText, parserKeywords: inputs.parserKeywords, ...defaultParserOptionsWrapper, }; return returnValue; } exports.createStatementParser = createStatementParser;