(
rawInputs: Readonly<CreateStatementParserInput<StateType, OutputType, ParserOptions>>,
)
| 46 | }; |
| 47 | |
| 48 | export function createStatementParser< |
| 49 | StateType, |
| 50 | OutputType extends ParsedOutput, |
| 51 | ParserOptions extends object | undefined = undefined, |
| 52 | >( |
| 53 | rawInputs: Readonly<CreateStatementParserInput<StateType, OutputType, ParserOptions>>, |
| 54 | ): Readonly<StatementParser<OutputType, ParserOptions>> { |
| 55 | const inputs: Readonly<CreateStatementParserInput<StateType, OutputType, ParserOptions>> = { |
| 56 | ...createStatementParserInputDefault, |
| 57 | ...rawInputs, |
| 58 | }; |
| 59 | |
| 60 | const pdfProcessing = inputs.pdfProcessing; |
| 61 | |
| 62 | if (!pdfProcessing) { |
| 63 | throw new Error('Missing pdf processing method'); |
| 64 | } |
| 65 | |
| 66 | const parseText: ParseTextFunction<OutputType, ParserOptions> = ({ |
| 67 | textLines, |
| 68 | parserOptions: inputParserOptions, |
| 69 | debug, |
| 70 | name, |
| 71 | }: ParseTextFunctionInput<ParserOptions>) => { |
| 72 | const stateMachineInputs: Readonly< |
| 73 | CreateStateMachineInput<StateType, OutputType, ParserOptions> |
| 74 | > = { |
| 75 | // ParserInitInput is a subtype of inputs' type |
| 76 | ...(inputs as ParserInitInput<StateType, OutputType, ParserOptions>), |
| 77 | name, |
| 78 | debug, |
| 79 | parserOptions: inputParserOptions, |
| 80 | }; |
| 81 | |
| 82 | const runStateMachine = createParserStateMachine<StateType, OutputType, ParserOptions>( |
| 83 | stateMachineInputs, |
| 84 | ); |
| 85 | |
| 86 | const output = runStateMachine(textLines); |
| 87 | |
| 88 | if (inputs.outputValidation) { |
| 89 | inputs.outputValidation(output); |
| 90 | } |
| 91 | |
| 92 | return output; |
| 93 | }; |
| 94 | |
| 95 | const convertPdfToText: ConvertPdfToTextFunction = async ( |
| 96 | filePath: string, |
| 97 | ): Promise<string[]> => { |
| 98 | const pdfPages = await pdfProcessing(filePath); |
| 99 | const textLines = flatten2dArray(pdfPages); |
| 100 | |
| 101 | return textLines; |
| 102 | }; |
| 103 | |
| 104 | const parsePdf: ParsePdfFunction<OutputType, ParserOptions> = async ({ |
| 105 | filePath, |
no outgoing calls
no test coverage detected