From 73de9ad97c9e01702526450439af50a31747eeb2 Mon Sep 17 00:00:00 2001 From: Simone Corsi Date: Fri, 8 Oct 2021 09:53:53 +0200 Subject: [PATCH] fix(table): rows are now correctly string[][] --- package-lock.json | 4 ++-- src/index.ts | 4 ++-- src/utils/common.ts | 12 ++++++------ test/common.ts | 2 +- test/index.ts | 2 +- types/index.d.ts | 2 +- types/utils/common.d.ts | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5c9a520..9545029 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scdev/declarative-markdown", - "version": "1.1.0", + "version": "1.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scdev/declarative-markdown", - "version": "1.1.0", + "version": "1.3.0", "license": "MIT", "devDependencies": { "@commitlint/cli": "^8.3.5", diff --git a/src/index.ts b/src/index.ts index ba50343..e7b0e56 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } diff --git a/src/utils/common.ts b/src/utils/common.ts index 766e7d2..1edb598 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -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; }; diff --git a/test/common.ts b/test/common.ts index 1207b63..d157235 100644 --- a/test/common.ts +++ b/test/common.ts @@ -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 |'; diff --git a/test/index.ts b/test/index.ts index 4cc55b3..1702fad 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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 }, diff --git a/types/index.d.ts b/types/index.d.ts index 15e06a8..093d5b2 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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, numbered?: boolean): this; tasks(items?: ListItems): this; } diff --git a/types/utils/common.d.ts b/types/utils/common.d.ts index 38194f2..28fd005 100644 --- a/types/utils/common.d.ts +++ b/types/utils/common.d.ts @@ -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;