Skip to content

Commit

Permalink
fix(table): rows are now correctly string[][]
Browse files Browse the repository at this point in the history
  • Loading branch information
simonecorsi committed Oct 8, 2021
1 parent f85c3ac commit 73de9ad
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class Markdown {
return this;
}

table(columns = [], rows = [], fmtFnc?): this {
this.addLine(table(columns, rows, fmtFnc));
table(columns = [], rows = []): this {
this.addLine(table(columns, rows));
return this;
}

Expand Down
12 changes: 6 additions & 6 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export const CR = '\n\n';
export const LB = '\n';

export const table = (
headers: string[] = [],
rows: string[] = [],
fmtFnc = (rowValue: any) => rowValue
) => {
export const table = (headers: string[] = [], rows: string[][] = []) => {
let t = '';
t += `| ${headers.join(' | ')} |${LB}|${' --- |'.repeat(headers.length)}`;
t += `${LB}| ${rows.map(fmtFnc).join(' | ')} |`;

for (const row of rows) {
t += `${LB}| ${row.join(' | ')} |`;
}

return t;
};

Expand Down
2 changes: 1 addition & 1 deletion test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tap.test('code', (t) => {

tap.test('table', (t) => {
const headers = ['id', 'name'];
const rows = ['1', 'Ajeje'];
const rows = [['1', 'Ajeje']];
let out = '| id | name |\n';
out += '| --- | --- |\n';
out += '| 1 | Ajeje |';
Expand Down
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tap.test('Shoud generate Markdown', async (t) => {
)}`
)
.header('Table', 2)
.table(['id', 'name'], ['1', 'Simone'])
.table(['id', 'name'], [['1', 'Simone']])
.header('List', 2)
.list([
{ text: 'list1', depth: 0 },
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export declare class Markdown {
paragraph(data: string | number): this;
header(title: string, n?: number): this;
image(filepath: string, altText?: string): this;
table(columns?: any[], rows?: any[], fmtFnc?: any): this;
table(columns?: any[], rows?: any[]): this;
list(items?: ListItems<ListItem>, numbered?: boolean): this;
tasks(items?: ListItems<TaskItem>): this;
}
2 changes: 1 addition & 1 deletion types/utils/common.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export declare const CR = "\n\n";
export declare const LB = "\n";
export declare const table: (headers?: string[], rows?: string[], fmtFnc?: (rowValue: any) => any) => string;
export declare const table: (headers?: string[], rows?: string[][]) => string;
export declare const italic: (text?: string) => string;
export declare const bold: (text?: string) => string;
export declare const link: (text: any, url: any) => string;
Expand Down

0 comments on commit 73de9ad

Please sign in to comment.