diff --git a/examples/make-table.ts b/examples/make-table.ts new file mode 100644 index 0000000..ace5f37 --- /dev/null +++ b/examples/make-table.ts @@ -0,0 +1,43 @@ +import ansis from 'ansis' +import terminalLink from 'terminal-link' + +import {TableOptions, makeTable} from '../src/index.js' + +const data = [ + { + age: 20, + employed: ansis.bold('true'), + id: terminalLink('36329', 'https://example.com/36329'), + name: 'Alice', + }, + { + age: 21, + employed: ansis.bold('true'), + id: terminalLink('49032', 'https://example.com/49032'), + name: ansis.dim('Bob'), + }, + { + age: 22, + employed: ansis.bold('false'), + id: terminalLink('51786', 'https://example.com/51786'), + name: 'Charlie', + }, +] + +const basic: TableOptions<(typeof data)[number]> = { + borderStyle: 'all', + columns: ['id', {key: 'name', name: 'First Name'}, 'age', 'employed'], + data, + headerOptions: { + bold: true, + color: '#905de8', + formatter: 'sentenceCase', + }, + sort: { + id: 'desc', + }, + verticalAlignment: 'center', +} + +const table = makeTable(basic) +console.log(table) diff --git a/src/index.ts b/src/index.ts index a31e89b..7db2aad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export {printTable, printTables} from './table.js' +export {makeTable, printTable, printTables} from './table.js' export type {TableOptions} from './types.js' diff --git a/src/table.tsx b/src/table.tsx index 75cf04f..625d637 100644 --- a/src/table.tsx +++ b/src/table.tsx @@ -546,8 +546,12 @@ function renderTableInChunks>(props: TableOpti } /** - * Renders a table with the given data. - * @param options see {@link TableOptions} + * Prints a table based on the provided options. If the data length exceeds 50,000 entries, + * the table is rendered in chunks to handle large datasets efficiently. + * + * @template T - A generic type that extends a record with string keys and unknown values. + * @param {TableOptions} options - The options for rendering the table, including data and other configurations. + * @returns {void} */ export function printTable>(options: TableOptions): void { if (options.data.length > 50_000) { @@ -561,6 +565,20 @@ export function printTable>(options: TableOpti output.maybePrintLastFrame() } +/** + * Generates a table as a string based on the provided options. + * + * @template T - A generic type extending a record with string keys and unknown values. + * @param {TableOptions} options - The options to configure the table. + * @returns {string} The rendered table as a string. + */ +export function makeTable>(options: TableOptions): string { + const output = new Output() + const instance = render(, {stdout: output.stream}) + instance.unmount() + return output.stream.lastFrame() ?? '' +} + function Container(props: ContainerProps) { return ( @@ -569,6 +587,14 @@ function Container(props: ContainerProps) { ) } +/** + * Prints multiple tables to the console. + * + * @template T - An array of records where each record represents a table. + * @param {Object.>} tables - An object containing table options for each table. + * @param {Omit} [options] - Optional container properties excluding 'children'. + * @throws {Error} Throws an error if the total number of rows across all tables exceeds 30,000. + */ export function printTables[]>( tables: {[P in keyof T]: TableOptions}, options?: Omit,