1.0.1 • Published 2 years ago

prettytable.js v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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;
ArgumentRequiredDescription
headerYesThe 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;
ArgumentRequiredDescription
footerYesThe 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[];
ArgumentRequiredDescription
indexYesThe index of the row

addRows

Adds list of rows to the table.

table.addRows([
  ['first row', 'value'],
  ['second row', 'value'],
]);
(rows: Cell[][]) => void;
ArgumentRequiredDescription
rowsYesThe list of rows

addRow

Adds row to the table.

table.addRow(['first row', 'value']);
(row: Cell[]) => void;
ArgumentRequiredDescription
rowYesThe row

deleteRow

Deletes row from table.

table.deleteRow(4);
(index: number) => void;
ArgumentRequiredDescription
indexYesThe 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;
ArgumentRequiredDefaultDescription
headerNonullThe header of the table
rowsNonullThe list of table rows
footerNonullThe 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;
ArgumentRequiredDefaultDescription
fileYesN\AThe content of CSV
optionsNo{ 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;
ArgumentRequiredDescription
jsonYesThe content of JSON

License

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