UNPKG

@accounter/shaam-uniform-format-generator

Version:

Fully typed application that generates, parses, and validates SHAAM uniform format tax reports (INI.TXT and BKMVDATA.TXT).

87 lines (86 loc) 2.86 kB
"use strict"; /** * Fixed-width formatting utilities */ Object.defineProperty(exports, "__esModule", { value: true }); exports.formatField = formatField; exports.encodeFixedWidth = encodeFixedWidth; exports.formatNumericField = formatNumericField; exports.joinFields = joinFields; exports.joinRecords = joinRecords; exports.joinLinesWithCRLF = joinLinesWithCRLF; exports.assembleFile = assembleFile; const newline_js_1 = require("../../format/newline.js"); const padding_js_1 = require("../../format/padding.js"); /** * Formats a field value with specified width and alignment * * @param value - The value to format * @param width - Target field width * @param align - Alignment ('left' or 'right') * @param padChar - Character to use for padding (default: space) * @returns Formatted fixed-width string */ function formatField(value, width, align, padChar = ' ') { if (align === 'left') { return (0, padding_js_1.padRight)(value, width, padChar); } return (0, padding_js_1.padLeft)(value, width, padChar); } /** * Encodes a value to fixed-width format with padding * * @param value - The value to encode * @param width - Target width * @param padChar - Padding character (default: space) * @param align - Alignment ('left' or 'right') * @returns Fixed-width encoded string */ function encodeFixedWidth(value, width, padChar = ' ', align = 'left') { const str = String(value); return formatField(str, width, align, padChar); } /** * Helper function to format numeric fields with zero padding */ function formatNumericField(value, width) { return (0, padding_js_1.padLeft)(value, width, '0'); } /** * Joins an array of field values into a single record line with CRLF ending * This is used by individual record encoders to create their output * * @param fields - Array of formatted field values * @returns Single record line ending with CRLF */ function joinFields(fields) { return fields.join('') + newline_js_1.CRLF; } /** * Joins an array of record lines that already have CRLF endings * * @param lines - Array of record lines to join (each line should already end with CRLF) * @returns Joined string */ function joinRecords(lines) { return lines.join(''); } /** * Joins an array of record lines and adds CRLF to each line * * @param lines - Array of record lines to join (without CRLF endings) * @returns Joined string with CRLF line endings */ function joinLinesWithCRLF(lines) { return lines.map(line => line + newline_js_1.CRLF).join(''); } /** * Creates a complete SHAAM format file by joining records * This is the main function for assembling final file content * * @param records - Array of encoded record strings (each already ending with CRLF) * @returns Complete file content ready for output */ function assembleFile(records) { return joinRecords(records); }