@thi.ng/markdown-table
Version:
Markdown table formatter/generator with support for column alignments
75 lines • 2.34 kB
TypeScript
import type { Fn, Keys } from "@thi.ng/api";
import type { Column, Row, TableOpts } from "./api.js";
/**
* Takes an array of column titles, an iterable of `rows` and (optional) table
* options (e.g. column alignments). Returns string of formatted Markdown table.
*
* @remarks
* Each `row` is a string array. Nullish cells/columns are allowed. Rows
* can also be empty. By default all columns are left-aligned.
*
* @example
* ```ts tangle:../export/table.ts
* import { table } from "@thi.ng/markdown-table";
*
* const res = table(
* ["ID", "Actor", "Comment"],
* [
* [1, "Alice"],
* [201, "Bob", "(foe)"],
* [3003, "Charlie", null],
* [44, "Dora", "(recipient)"],
* ],
* { bold: true, align: ["r", "c", "l"] }
* );
*
* console.log(res);
* // | **ID** | **Actor** | **Comment** |
* // |-------:|:---------:|:------------|
* // | 1 | Alice | |
* // | 201 | Bob | (foe) |
* // | 3003 | Charlie | |
* // | 44 | Dora | (recipient) |
* ```
*
* @param header -
* @param rows -
* @param opts -
*/
export declare const table: (header: string[], rows: Iterable<Row>, opts?: Partial<TableOpts>) => string;
/**
* Similar to {@link table}, however accepts rows as objects and looks up column
* values using given `keys` array.
*
* @example
* ```ts tangle:../export/table-keys.ts
* import { tableKeys } from "@thi.ng/markdown-table";
*
* const res = tableKeys(
* ["ID", "Actor", "Comment"],
* ["id", "name", (x) => x.hint],
* [
* { id: 1, name: "Alice" },
* { id: 201, name: "Bob", hint: "(foe)" },
* { id: 3003, name: "Charlie" },
* { id: 44, name: "Dora", hint: "(recipient)" },
* ],
* { bold: true, align: ["r", "c", "l"] }
* );
*
* console.log(res);
* // | **ID** | **Actor** | **Comment** |
* // |-------:|:---------:|:------------|
* // | 1 | Alice | |
* // | 201 | Bob | (foe) |
* // | 3003 | Charlie | |
* // | 44 | Dora | (recipient) |
* ```
*
* @param headers -
* @param keys -
* @param items -
* @param opts -
*/
export declare const tableKeys: <T>(headers: string[], keys: (Keys<T> | Fn<T, Column>)[], items: Iterable<T>, opts?: Partial<TableOpts>) => string;
//# sourceMappingURL=table.d.ts.map