UNPKG

@accounter/shaam6111-generator

Version:

Fully typed application that generates, parses, and validates SHAAM 6111 tax reports.

74 lines (73 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertReportToFile = convertReportToFile; exports.readReportFromFile = readReportFromFile; const generate_report_js_1 = require("../generators/generate-report.js"); const index_js_1 = require("../parsers/index.js"); const index_js_2 = require("../validation/index.js"); const encoding_js_1 = require("./encoding.js"); /** * Converts a SHAAM6111 report to a file with proper Windows-1255 encoding. * @param report The report string or ReportData object to convert. * @param fileName The name of the file to create. * @param validate If true, validates the report before converting. * @returns A File object containing the encoded report. */ function convertReportToFile(report, fileName = 'SHAAM6111.dat', validate = true) { // If report is a ReportData object, convert it to a string let reportString; if (typeof report === 'string') { if (validate) { (0, index_js_2.validateReport)(report); } reportString = report; } else { reportString = (0, generate_report_js_1.generateReport)(report, validate); } // Convert the report string to Windows-1255 encoding let encodedReport; try { encodedReport = (0, encoding_js_1.toWindows1255)(reportString); } catch (error) { throw new Error(`Failed to encode report to Windows-1255: ${error.message}`, { cause: error, }); } // Create and return a File object with .dat extension return new File([new Uint8Array(encodedReport)], fileName, { type: 'application/octet-stream', }); } /** * Reads a SHAAM6111 report from a file with proper Windows-1255 decoding. * @param reportFile The file to read the report from. * @param parseToObject If true, parses the report into a ReportData object; if false, returns the raw report string. * @param validate If true, validates the report after decoding. * @returns A Promise that resolves to either the raw report string or a parsed ReportData object. */ async function readReportFromFile(reportFile, parseToObject = false, validate = true) { // Decode the buffer from Windows-1255 encoding const arrayBuffer = await reportFile.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); let reportString; try { reportString = (0, encoding_js_1.fromWindows1255)(buffer); } catch (error) { throw new Error(`Failed to decode report from Windows-1255: ${error.message}`, { cause: error, }); } if (parseToObject) { // Return a parsed ReportData object return (0, index_js_1.parseReport)(reportString, validate); } // If validate is true, validate the report string if (validate) { (0, index_js_2.validateReport)(reportString); } // Return the raw string; return reportString; }