RosettaCodeData/Task/CSV-to-HTML-translation/JavaScript/csv-to-html-translation-4.js
2025-02-27 18:35:13 -05:00

40 lines
1 KiB
JavaScript

function csvToHtml(input) {
if (!input || typeof input !== 'string') {
throw new Error('Invalid input!');
}
function _createTableCell(cellContents, index) {
if (!cellContents || typeof cellContents !== 'string') {
throw new Error('Invalid data!');
}
const tableCell = document.createElement(
(index === 0) ? 'th' : 'td'
);
tableCell.textContent = cellContents.trim();
return tableCell;
}
const rows = input.split('\n');
const table = document.createElement('table');
rows.forEach((row, index) => {
const tableRow = document.createElement('tr');
const tableCells = row.split(',');
tableCells.forEach((cell) => {
try {
tableRow.appendChild(
_createTableCell(cell, index),
);
} catch (error) {
console.error(error);
}
});
table.appendChild(tableRow);
});
return table;
}