prettytable.js
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published
PrettyTable.js logo

NPM version NPM downloads per month build status license

JavaScript / TypeScript library for Node.JS and browsers to easily creating ASCII tables

About

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.

Table of contents

Installation

Node.js 14.0.0 or newer supported.

Install with the npm:

npm install prettytable.js

Install with the yarn:

yarn add prettytable.js

Usage

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 |
+-----------+-----+-------------+

Table methods

Note
type Cell = string | number | null | undefined;

getHeader

Gets header of the table.

table.getHeader();
() => Cell[];

setHeader

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

getFooter

Gets footer of the table.

table.getFooter();
() => Cell[];

setFooter

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

getRows

Gets all rows of the table.

table.getRows();
() => Cell[][];

getRow

Gets row of the table.

table.getRow(index);
(index: number) => Cell[];
Argument Required Description
index Yes The index of the row

addRows

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

addRow

Adds row to the table.

table.addRow(['first row', 'value']);
(row: Cell[]) => void;
Argument Required Description
row Yes The row

deleteRow

Deletes row from table.

table.deleteRow(4);
(index: number) => void;
Argument Required Description
index Yes The row index for deletion

toString

Converts table to string representation.

table.toString();
() => string;

toCsv

Converts table to CSV representation.

table.toCsv();
() => string;

toJson

Converts table to JSON representation.

table.toJson();
() => Record<string | number, Cell>[];

clone

Clones table.

table.clone();
() => PrettyTable;

static from

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

static fromCsv

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

static fromJson

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

License

Distributed under the MIT License. See LICENSE for more information.

Package Sidebar

Install

npm i prettytable.js

Weekly Downloads

13

Version

1.0.1

License

MIT

Unpacked Size

38.5 kB

Total Files

29

Last publish

Collaborators

  • mrrefactoring