UNPKG

@accounter/pcn874-generator

Version:
59 lines (58 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pcnGenerator = void 0; exports.validatePcn874 = validatePcn874; const tslib_1 = require("tslib"); const index_js_1 = require("./utils/index.js"); const pcnGenerator = (headerInfo, transactions, options = {}) => { let textFile = ''; const { header, footer } = (0, index_js_1.headerInfoToHeaderAndFooterStrings)(headerInfo, options); textFile += header; // sort transactions const sortedTransactions = (0, index_js_1.sortTransactions)(transactions, options); // handle transactions sortedTransactions.map(transaction => { textFile += (0, index_js_1.transactionToString)(transaction, options); }); textFile += footer; return textFile; }; exports.pcnGenerator = pcnGenerator; function validatePcn874(content) { const lines = content.split('\n'); if (lines.length < 3) { return false; } // TODO: this is a very basic validation, we should add more checks // validate header const header = lines[0]; if (header.length !== 131) { return false; } if (header[0] !== 'O') { return false; } if (header[16] !== '1') { return false; } // validate footer const footer = lines[lines.length - 1]; if (footer.length !== 10) { return false; } if (footer[0] !== 'X') { return false; } // validate transaction lines const transactionLines = lines.slice(1, -1); for (const line of transactionLines) { if (line.length !== 60) { return false; } if (!['C', 'H', 'I', 'K', 'L', 'M', 'P', 'R', 'S', 'T', 'Y'].includes(line[0])) { return false; } } return true; } tslib_1.__exportStar(require("./types.js"), exports);