Creates ASCII tables from multiple data sources. The table can be populated by adding table rows one by one, by multiple, from a CSV file or from a JSON file.
Node.js 14.0.0 or newer supported.
Install with the npm:
npm install prettytable.js
Install with the yarn:
yarn add prettytable.js
Working with PrettyTable.js you can start from follow small code snippet:
import { PrettyTable } from 'prettytable.js';
const table = new PrettyTable();
table.setHeader(['Name', 'Age', 'City']);
table.addRows([
['John', 22, 'New York'],
['Elizabeth', 43, 'Chicago'],
['Bill', 31, 'Atlanta'],
['Mary', 18, 'Los Angeles'],
]);
console.log(table.toString());
This gives you the following table on console:
+-----------+-----+-------------+
| Name | Age | City |
+-----------+-----+-------------+
| John | 22 | New York |
| Elizabeth | 43 | Chicago |
| Bill | 31 | Atlanta |
| Mary | 18 | Los Angeles |
+-----------+-----+-------------+
type Cell = string | number | null | undefined;
Gets header of the table.
table.getHeader();
() => Cell[];
Sets header to the table.
table.setHeader(['Name', 'Age', 'City']);
(header: Cell[]) => void;
Argument | Required | Description |
---|---|---|
header |
Yes | The header to be settled to the table |
Gets footer of the table.
table.getFooter();
() => Cell[];
Sets header to the table.
table.setFooter(['Name', 'Age', 'City']);
(footer: Cell[]) => void;
Argument | Required | Description |
---|---|---|
footer |
Yes | The footer to be settled to the table |
Gets all rows of the table.
table.getRows();
() => Cell[][];
Gets row of the table.
table.getRow(index);
(index: number) => Cell[];
Argument | Required | Description |
---|---|---|
index |
Yes | The index of the row |
Adds list of rows to the table.
table.addRows([
['first row', 'value'],
['second row', 'value'],
]);
(rows: Cell[][]) => void;
Argument | Required | Description |
---|---|---|
rows |
Yes | The list of rows |
Adds row to the table.
table.addRow(['first row', 'value']);
(row: Cell[]) => void;
Argument | Required | Description |
---|---|---|
row |
Yes | The row |
Deletes row from table.
table.deleteRow(4);
(index: number) => void;
Argument | Required | Description |
---|---|---|
index |
Yes | The row index for deletion |
Converts table to string representation.
table.toString();
() => string;
Converts table to CSV representation.
table.toCsv();
() => string;
Converts table to JSON representation.
table.toJson();
() => Record<string | number, Cell>[];
Clones table.
table.clone();
() => PrettyTable;
Creates table from arguments.
const table = PrettyTable.from(
['header 1', 'header 2'],
[ ['row 1', 'row 1'] ],
['footer 1', 'footer 1'],
);
(
header?: Cell[] | null,
rows?: Cell[][] | null,
footer?: Cell[] | null,
) => PrettyTable;
Argument | Required | Default | Description |
---|---|---|---|
header |
No | null |
The header of the table |
rows |
No | null |
The list of table rows |
footer |
No | null |
The footer of the table |
Creates table from CSV.
const table = PrettyTable.fromCsv(csvFile, { header: true });
(file: string | Buffer, options?: { header?: boolean; footer?: boolean; }) => PrettyTable;
Argument | Required | Default | Description |
---|---|---|---|
file |
Yes | N\A | The content of CSV |
options |
No | { header: true, footer: false } |
The options of CSV table |
Creates table from JSON.
const table = PrettyTable.fromJson([
{
"name": "john",
"age": 22,
"city": "new york"
},
{
"name": "elizabeth",
"age": 43,
"city": "chicago"
},
{
"name": "bill",
"age": 31,
"city": "atlanta"
},
{
"name": "mary",
"age": 18,
"city": "los angeles"
}
]);
(json: Record<string | number, Cell>[]) => PrettyTable;
Argument | Required | Description |
---|---|---|
json |
Yes | The content of JSON |
Distributed under the MIT License. See LICENSE
for more information.