Skip to content

Commit

Permalink
Rename convertTextToTableMagic to textToTable.
Browse files Browse the repository at this point in the history
Refactoring of IITC.utils.textToTable
  • Loading branch information
modos189 committed Nov 9, 2024
1 parent 3130b98 commit f46cfdf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
56 changes: 25 additions & 31 deletions core/code/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,39 +334,33 @@ const genFourColumnTable = function (blocks) {
* Converts text with newlines (`\n`) and tabs (`\t`) into an HTML table.
*
* @memberof IITC.utils
* @function convertTextToTableMagic
* @function textToTable
* @param {string} text - The text to convert.
* @returns {string} The resulting HTML table.
*/
const convertTextToTableMagic = function (text) {
// check if it should be converted to a table
if (!text.match(/\t/)) return text.replace(/\n/g, '<br>');

var data = [];
var columnCount = 0;

// parse data
var rows = text.split('\n');
$.each(rows, function (i, row) {
data[i] = row.split('\t');
if (data[i].length > columnCount) columnCount = data[i].length;
});
const textToTable = function (text) {
// If no tabs are present, replace newlines with <br> and return
if (!text.includes('\t')) return text.replace(/\n/g, '<br>');

// Split text into rows and columns, tracking the max column count
const rows = text.split('\n').map((row) => row.split('\t'));
const columnCount = Math.max(...rows.map((row) => row.length));

// Build the table rows
const tableRows = [];
for (const row of rows) {
let rowHtml = '<tr>';
for (let k = 0; k < row.length; k++) {
const cell = IITC.utils.escapeHtml(row[k]);
const colspan = k === 0 && row.length < columnCount ? ` colspan="${columnCount - row.length + 1}"` : '';
rowHtml += `<td${colspan}>${cell}</td>`;
}
rowHtml += '</tr>';
tableRows.push(rowHtml);
}

// build the table
var table = '<table>';
$.each(data, function (i) {
table += '<tr>';
$.each(data[i], function (k, cell) {
var attributes = '';
if (k === 0 && data[i].length < columnCount) {
attributes = ' colspan="' + (columnCount - data[i].length + 1) + '"';
}
table += '<td' + attributes + '>' + cell + '</td>';
});
table += '</tr>';
});
table += '</table>';
return table;
// Combine all rows into a single table HTML
return `<table>${tableRows.join('')}</table>`;
};

/**
Expand Down Expand Up @@ -461,7 +455,7 @@ IITC.utils = {
prettyEnergy,
uniqueArray,
genFourColumnTable,
convertTextToTableMagic,
textToTable,
clamp,
clampLatLng,
clampLatLngBounds,
Expand All @@ -488,7 +482,7 @@ const legacyFunctionMappings = {
prettyEnergy: 'prettyEnergy',
uniqueArray: 'uniqueArray',
genFourColumnTable: 'genFourColumnTable',
convertTextToTableMagic: 'convertTextToTableMagic',
convertTextToTableMagic: 'textToTable',
clamp: 'clamp',
clampLatLng: 'clampLatLng',
clampLatLngBounds: 'clampLatLngBounds',
Expand Down
48 changes: 48 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,54 @@ describe('IITC.utils.genFourColumnTable', () => {
});
});

describe('IITC.utils.textToTable', () => {
it('should return text with BR instead of \\n if no tabs are present', () => {
const text = 'Line1\nLine2\nLine3';
const result = IITC.utils.textToTable(text);
expect(result).to.equal('Line1<br>Line2<br>Line3');
});

it('should create a table with one row and two columns for a single tab-separated line', () => {
const text = 'Cell1\tCell2';
const result = IITC.utils.textToTable(text);
const check = `<table>` + `<tr><td>Cell1</td><td>Cell2</td></tr>` + `</table>`;
expect(result).to.equal(check);
});

it('should create a table with multiple rows and columns for text with multiple lines and tabs', () => {
const text = 'R1C1\tR1C2\nR2C1\tR2C2\nR3C1\tR3C2';
const result = IITC.utils.textToTable(text);
const check =
`<table>` + `<tr><td>R1C1</td><td>R1C2</td></tr>` + `<tr><td>R2C1</td><td>R2C2</td></tr>` + `<tr><td>R3C1</td><td>R3C2</td></tr>` + `</table>`;
expect(result).to.equal(check);
});

it('should add colspan to rows with fewer columns than the longest row', () => {
const text = 'R1C1\tR1C2\tR1C3\nR2C1\tR2C2\nR3C1';
const result = IITC.utils.textToTable(text);
const check =
`<table>` +
`<tr><td>R1C1</td><td>R1C2</td><td>R1C3</td></tr>` +
`<tr><td colspan="2">R2C1</td><td>R2C2</td></tr>` +
`<tr><td colspan="3">R3C1</td></tr>` +
`</table>`;
expect(result).to.equal(check);
});

it('should handle empty input text as is', () => {
const text = '';
const result = IITC.utils.textToTable(text);
expect(result).to.equal('');
});

it('should escape HTML special characters within cells', () => {
const text = 'Cell1\tCell<2>\nCell&3\tCell"4"';
const result = IITC.utils.textToTable(text);
const check = `<table>` + `<tr><td>Cell1</td><td>Cell&lt;2&gt;</td></tr>` + `<tr><td>Cell&amp;3</td><td>Cell&quot;4&quot;</td></tr>` + `</table>`;
expect(result).to.equal(check);
});
});

describe('IITC.utils.clamp', () => {
it('should return the value itself if it is within the range', () => {
const result = IITC.utils.clamp(5, 10, -10);
Expand Down

0 comments on commit f46cfdf

Please sign in to comment.