mirror of
https://github.com/discourse/discourse-table-builder.git
synced 2026-09-17 23:32:14 -04:00
FIX: Allow headings to have the same label (#46)
Fixes an issue where multiple headings with the same value would remove columns from displaying.
This commit is contained in:
Vendored
-1
@@ -1 +0,0 @@
|
||||
export default `|Make | Model | Year|\r\n|--- | --- | ---|\r\n|Toyota | Supra | 1998|\r\n|Nissan | Skyline | 1999|\r\n|Honda | S2000 | 2001|\r\n`;
|
||||
@@ -1 +0,0 @@
|
||||
export default `|Make | Model | Price|\r\n|--- | --- | ---|\r\n|Toyota | Supra | $50,000|\r\n| | Celica | $20,000|\r\n|Nissan | GTR | $80,000|\r\n`;
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
export const mdTable = `|Make | Model | Year|\r\n|--- | --- | ---|\r\n|Toyota | Supra | 1998|\r\n|Nissan | Skyline | 1999|\r\n|Honda | S2000 | 2001|\r\n`;
|
||||
export const mdTableSpecialChars = `|Make | Model | Price|\r\n|--- | --- | ---|\r\n|Toyota | Supra | $50,000|\r\n| | Celica | $20,000|\r\n|Nissan | GTR | $80,000|\r\n`;
|
||||
export const mdTableNonUniqueHeadings = `|col1 | col2 | col1|\r\n|--- | --- | ---|\r\n|Col A | Col B | Col C|\r\n`;
|
||||
@@ -1,7 +1,11 @@
|
||||
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
|
||||
import { test } from "qunit";
|
||||
import mdTableFixture from "../../fixtures/md-table-fixture";
|
||||
import mdTableSpecialCharsFixture from "../../fixtures/md-table-special-chars-fixture";
|
||||
import {
|
||||
mdTable,
|
||||
mdTableNonUniqueHeadings,
|
||||
mdTableSpecialChars,
|
||||
} from "../../fixtures/md-table";
|
||||
|
||||
import {
|
||||
arrayToTable,
|
||||
findTableRegex,
|
||||
@@ -11,51 +15,80 @@ discourseModule("Unit | Utilities", function () {
|
||||
test("arrayToTable", function (assert) {
|
||||
const tableData = [
|
||||
{
|
||||
Make: "Toyota",
|
||||
Model: "Supra",
|
||||
Year: "1998",
|
||||
col0: "Toyota",
|
||||
col1: "Supra",
|
||||
col2: "1998",
|
||||
},
|
||||
{
|
||||
Make: "Nissan",
|
||||
Model: "Skyline",
|
||||
Year: "1999",
|
||||
col0: "Nissan",
|
||||
col1: "Skyline",
|
||||
col2: "1999",
|
||||
},
|
||||
{
|
||||
Make: "Honda",
|
||||
Model: "S2000",
|
||||
Year: "2001",
|
||||
col0: "Honda",
|
||||
col1: "S2000",
|
||||
col2: "2001",
|
||||
},
|
||||
];
|
||||
|
||||
assert.strictEqual(
|
||||
arrayToTable(tableData),
|
||||
mdTableFixture,
|
||||
arrayToTable(tableData, ["Make", "Model", "Year"]),
|
||||
mdTable,
|
||||
"it creates a markdown table from an array of objects (with headers as keys)"
|
||||
);
|
||||
|
||||
const specialCharsTableData = [
|
||||
{
|
||||
Make: "Toyota",
|
||||
Model: "Supra",
|
||||
Price: "$50,000",
|
||||
col0: "Toyota",
|
||||
col1: "Supra",
|
||||
col2: "$50,000",
|
||||
},
|
||||
{
|
||||
Make: "",
|
||||
Model: "Celica",
|
||||
Price: "$20,000",
|
||||
col0: "",
|
||||
col1: "Celica",
|
||||
col2: "$20,000",
|
||||
},
|
||||
{
|
||||
Make: "Nissan",
|
||||
Model: "GTR",
|
||||
Price: "$80,000",
|
||||
col0: "Nissan",
|
||||
col1: "GTR",
|
||||
col2: "$80,000",
|
||||
},
|
||||
];
|
||||
|
||||
assert.strictEqual(
|
||||
arrayToTable(specialCharsTableData),
|
||||
mdTableSpecialCharsFixture,
|
||||
arrayToTable(specialCharsTableData, ["Make", "Model", "Price"]),
|
||||
mdTableSpecialChars,
|
||||
"it creates a markdown table with special characters in correct alignment"
|
||||
);
|
||||
|
||||
const nonUniqueColumns = ["col1", "col2", "col1"];
|
||||
|
||||
assert.strictEqual(
|
||||
arrayToTable(
|
||||
[{ col0: "Col A", col1: "Col B", col2: "Col C" }],
|
||||
nonUniqueColumns
|
||||
),
|
||||
mdTableNonUniqueHeadings,
|
||||
"it does not suppress a column if heading is the same as another column"
|
||||
);
|
||||
});
|
||||
test("arrayToTable with custom column prefix", function (assert) {
|
||||
const tableData = [
|
||||
{
|
||||
A0: "hey",
|
||||
A1: "you",
|
||||
},
|
||||
{
|
||||
A0: "over",
|
||||
A1: "there",
|
||||
},
|
||||
];
|
||||
|
||||
assert.strictEqual(
|
||||
arrayToTable(tableData, ["Col 1", "Col 2"], "A"),
|
||||
`|Col 1 | Col 2|\r\n|--- | ---|\r\n|hey | you|\r\n|over | there|\r\n`,
|
||||
"it works"
|
||||
);
|
||||
});
|
||||
|
||||
test("findTableRegex", function (assert) {
|
||||
|
||||
Reference in New Issue
Block a user