Skip to content

Commit e780566

Browse files
Merge pull request #15 from SimplrJS/dev
Version `1.0.5`.
2 parents b25c2f4 + 5236cb7 commit e780566

File tree

5 files changed

+99
-16
lines changed

5 files changed

+99
-16
lines changed

__tests__/__snapshots__/markdown-generator.test.ts.snap

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__tests__/markdown-generator.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,53 @@ describe("Table", () => {
275275
expect(result).toMatchSnapshot();
276276
});
277277

278+
it("Table with a single column filled.", () => {
279+
const headers = ["Name", "Constraint type", "Default type"];
280+
const rows = [
281+
["TValue", "", ""]
282+
];
283+
284+
const result = MarkdownGenerator.Table(headers, rows, { removeColumnIfEmpty: true });
285+
expect(result).toMatchSnapshot();
286+
});
287+
288+
it("Table with an empty column in the middle.", () => {
289+
const headers = ["Name", "Constraint type", "Default type"];
290+
const rows = [
291+
["TValue", "", "string"],
292+
["T", "", "string"],
293+
["TKey ", "", "string"],
294+
];
295+
296+
const result = MarkdownGenerator.Table(headers, rows, { removeColumnIfEmpty: true });
297+
expect(result).toMatchSnapshot();
298+
});
299+
300+
it("Table with empty rows", () => {
301+
const headers = ["Name", "Constraint type", "Default type", "InitialValue"];
302+
const rows = [
303+
["TValue", "", "", "{}"],
304+
["", "", "", ""],
305+
["TKey", "", "", "{}"],
306+
["", "", "", ""]
307+
];
308+
309+
const result = MarkdownGenerator.Table(headers, rows, { removeColumnIfEmpty: true, removeRowIfEmpty: true });
310+
expect(result).toMatchSnapshot();
311+
});
312+
313+
it("Table with rows that has a single cell filled", () => {
314+
const headers = ["Name", "Constraint type", "Default type", "InitialValue"];
315+
const rows = [
316+
["TValue", "", "", ""],
317+
["", "Object", "", ""],
318+
["", "", "Object", ""],
319+
["", "", "", "{}"]
320+
];
321+
322+
const result = MarkdownGenerator.Table(headers, rows, { removeColumnIfEmpty: true });
323+
});
324+
278325
it("Simple example with mixed alignments", () => {
279326
const headers: Array<TableHeader | string> = [
280327
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@simplrjs/markdown",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Markdown generator",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/contracts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type UnorderedListSymbols = "*" | "+" | "-";
1414

1515
export interface TableOptions {
1616
removeColumnIfEmpty?: boolean;
17+
removeRowIfEmpty?: boolean;
1718
}
1819

1920
export interface TableHeader {

src/generators/table-generator.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export namespace TableGenerator {
1111
export function RenderTable(headers: Array<string | TableHeader>, content: string[][], options?: TableOptions): string[] {
1212
const columnsWidths: number[] = [];
1313
const removeColumnIfEmpty = options != null && options.removeColumnIfEmpty;
14+
const removeRowIfEmpty = options != null && options.removeRowIfEmpty;
1415

1516
let filledRows = content.map(row => {
1617
if (row.length < headers.length) {
@@ -20,28 +21,35 @@ export namespace TableGenerator {
2021
}
2122
});
2223

23-
const finalHeaders = headers
24-
.map((header, headerIndex) => {
25-
const rows = filledRows.map(x => x[headerIndex]);
26-
columnsWidths[headerIndex] = GetMaxColumnWidth(GetHeaderText(header), rows, removeColumnIfEmpty);
24+
// Removing empty rows if option `removeRowIfEmpty` enabled.
25+
if (removeRowIfEmpty) {
26+
filledRows = filledRows.filter(row => !row.every(cell => cell.length === 0));
27+
}
2728

28-
if (removeColumnIfEmpty && columnsWidths[headerIndex] === 0) {
29-
filledRows = filledRows
30-
.filter(x => x[headerIndex] != null)
31-
.map(x => x.splice(0, headerIndex));
29+
// Indexes of empty columns, that should be removed.
30+
const columnsToRemove: number[] = [];
3231

33-
return null;
34-
}
32+
// Filling columns widths.
33+
headers.forEach((header, index) => {
34+
// Getting rows of a single column
35+
const rows = filledRows.map(x => x[index]);
36+
columnsWidths[index] = GetMaxColumnWidth(GetHeaderText(header), rows, removeColumnIfEmpty);
37+
38+
if (removeColumnIfEmpty && columnsWidths[index] === 0) {
39+
columnsToRemove.push(index);
40+
}
41+
});
3542

36-
return header;
37-
})
38-
.filter(x => x != null) as Array<string | TableHeader>;
43+
// Removing unnecessary columns.
44+
const finalHeaders = headers.filter((x, index) => columnsToRemove.indexOf(index) === -1);
45+
const finalRows = filledRows.map(row => row.filter((x, index) => columnsToRemove.indexOf(index) === -1));
46+
const finalColumnsWidths = columnsWidths.filter((x, index) => columnsToRemove.indexOf(index) === -1);
3947

4048
const lines = [
4149
// Header
42-
...RenderTableHeader(finalHeaders, columnsWidths),
50+
...RenderTableHeader(finalHeaders, finalColumnsWidths),
4351
// Content
44-
...RenderTableContents(filledRows, columnsWidths)
52+
...RenderTableContents(finalRows, finalColumnsWidths)
4553
];
4654

4755
return lines;

0 commit comments

Comments
 (0)