UNPKG

json-to-table-format

Version:
198 lines (197 loc) 7.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getData = exports.getStartPoints = exports.getStartPoint = exports.getRows = exports.getRow = exports.removeDoubleQuotes = exports.addSpaces = void 0; const constant_1 = require("./constant"); function addSpaces(string, spaceCount) { for (let index = 0; index < spaceCount; index++) { string += constant_1.WS; } return string; } exports.addSpaces = addSpaces; function removeDoubleQuotes(value) { return value.replace(/"/g, ' '); } exports.removeDoubleQuotes = removeDoubleQuotes; // Fixed ASCII border creation function createAsciiBorder(startPoints) { const border = startPoints.map((point, index) => { if (index === 0) return '-'.repeat(startPoints[1] - startPoints[0]); if (index === startPoints.length - 1) return ''; return '-'.repeat(startPoints[index + 1] - startPoints[index]); }).filter(Boolean).join('+'); return '+' + border + '+'; } // Format complex values for better display function formatComplexValue(value) { if (value === null || value === undefined) return 'null'; if (typeof value !== 'object') return String(value); if (Array.isArray(value)) { if (value.length === 0) return '[]'; return value.map(item => { if (typeof item === 'object' && item !== null) { return formatComplexValue(item); } return String(item); }).join('\n'); } const entries = Object.entries(value) .map(([k, v]) => { if (typeof v === 'object' && v !== null) { return `${k}: ${formatComplexValue(v)}`; } return `${k}: ${v}`; }) .join('\n'); return `{${entries}}`; } function wrapText(text, maxWidth) { if (!text || text.length <= maxWidth) return [text || '']; // Split by newlines first to preserve array/object structure const lines = text.split('\n'); const wrappedLines = []; lines.forEach(line => { if (line.length <= maxWidth) { wrappedLines.push(line); } else { const words = line.split(' '); let currentLine = ''; words.forEach(word => { if ((currentLine + word).length <= maxWidth) { currentLine += (currentLine ? ' ' : '') + word; } else { if (currentLine) wrappedLines.push(currentLine); currentLine = word; } }); if (currentLine) wrappedLines.push(currentLine); } }); return wrappedLines; } function getRow(headers, startPoints, isHeaderRow, obj) { const maxWidth = 25; const rows = []; let maxLines = 1; // First pass: calculate max lines needed headers.forEach((header, index) => { const value = isHeaderRow ? header : obj[header]; const formattedValue = typeof value === 'object' ? formatComplexValue(value) : String(value); const wrappedLines = wrapText(formattedValue, maxWidth); maxLines = Math.max(maxLines, wrappedLines.length); }); // Generate all lines for the row for (let lineIndex = 0; lineIndex < maxLines; lineIndex++) { let row = constant_1.ES; headers.forEach((header, index) => { const value = isHeaderRow ? header : obj[header]; const formattedValue = typeof value === 'object' ? formatComplexValue(value) : String(value); const wrappedLines = wrapText(formattedValue, maxWidth); const lineContent = wrappedLines[lineIndex] || ''; row += lineContent; const spaceCount = startPoints[index + 1] - row.length; row = addSpaces(row, spaceCount); }); rows.push(row); } return rows; } exports.getRow = getRow; function getRows(data, headers, startPoints, style = 'simple') { let rows = []; if (style === 'ascii') { // Add top border rows.push(createAsciiBorder(startPoints)); // Add header row with borders const headerRows = getRow(headers, startPoints, true); headerRows.forEach(row => { rows.push('|' + row + '|'); }); // Add separator rows.push(createAsciiBorder(startPoints)); // Add data rows with borders data.forEach(obj => { const dataRows = getRow(headers, startPoints, false, obj); dataRows.forEach(row => { rows.push('|' + row + '|'); }); }); // Add bottom border rows.push(createAsciiBorder(startPoints)); } else { // Original simple style const headerRows = getRow(headers, startPoints, true); rows.push(...headerRows); data.forEach(obj => { const dataRows = getRow(headers, startPoints, false, obj); rows.push(...dataRows); }); } return rows; } exports.getRows = getRows; function getStartPoint(header, data, startPoint) { let lengths = [header.length]; data.forEach(obj => { if (obj[header] === null) { lengths.push(4); } else { const value = obj[header]; const formattedValue = typeof value === 'object' ? formatComplexValue(value) : value; lengths.push(String(formattedValue).length); } }); startPoint += Math.max(...lengths) + 5; return startPoint; } exports.getStartPoint = getStartPoint; function getStartPoints(data, headers) { let startPoints = [0]; let startPoint = 0; headers.forEach(header => { let lengths = [header.length]; data.forEach(obj => { if (obj[header] === null) { lengths.push(4); } else { const value = obj[header]; const formattedValue = typeof value === 'object' ? formatComplexValue(value) : String(value); // Calculate width based on wrapped lines const wrappedLines = wrapText(formattedValue, 25); const maxLineLength = Math.max(...wrappedLines.map(line => line.length)); lengths.push(maxLineLength); } }); // Add padding for better readability startPoint += Math.max(...lengths) + 4; startPoints.push(startPoint); }); return startPoints; } exports.getStartPoints = getStartPoints; function getData(params) { try { if (!Array.isArray(params)) { params = [params]; } return params.map((param) => JSON.parse(JSON.stringify(param))); } catch (error) { console.error("Error parsing data:", error); return null; } } exports.getData = getData;