nd-table
Node.js Table builder for:
- console/terminal (ascii, utf8, ansi)
- MarkDown (md)
- HTML
- JSON
- CSV, TSV
- pre-rendered data for your custom outputs
Supported features
Depending on the output format:
- borders
- multiline cells
- horizontal and vertical cell alignments
- styles: color, bold, italic
See details at Configuration options
Install
npm install nd-table
In your code
import { Table } from 'nd-table';
Or, if you use require()
:
const Table = require('nd-table').Table;
Usage
const table = new Table('Column A', 'Column B');
table.addRowWithHead('First Row Head', 'abc def\nghi jkl mno pqr', 1);
table.addRowWithHead('Row Head', 'stu', 2);
table.addRowWithHead('Another Row Head', { value: 'vwx', bold: true, color: 'red' }, 3);
table.addRow('yz', { value: 4, link: 'https://osnews.com/' });
console.log(table.toString());
Builder API
Table.fromData(data[][]);
table.columns;
table.rows;
table.addRow(...values);
table.addRowWithHead(head, ...values);
table.addSeparator();
table.clear();
table.deleteColumn(x);
table.deleteColumns(x, count);
table.deleteRow(y);
table.deleteRows(y, count);
table.flip();
table.getCell(x, y);
table.getColumnHeaders();
table.getRowHeaders();
table.getSnapshot();
table.insertColumn(x);
table.insertColumns(x, count);
table.insertRow(y, ...values);
table.insertRowWithHead(y, head, ...values);
table.insertRows(y, count);
table.replaceRow(y, ...values);
table.replaceRowWithHead(y, head, ...values);
table.setCell(x, y, value);
table.setColumnHeaders(newHeaders);
table.setRowHeaders(newHeaders: CellValueOrValueWithOptions[] | null | undefined);
Cell Value
- Value can be any primitive:
undefined
, null
, boolean
, number
, bigint
, or string
- Objects (including arrays), functions, and symbols will be stringified.
- Values (marked with
*
above) can be passed directly, or in combination with config options, like this:
table.addRow({ value: 'My Cell Value', align: 'right', borderLeft: true }, 'Other Cell Value');
Coordinates: x, y
Columns and rows are in a 0-based index that excludes the optional headers
x = -1 is the row header
y = -1 is the column header
Config API
getCellConfig(x, y);
getCellRenderConfig(x, y);
getColumnConfig(x);
getRowConfig(y);
getTableConfig();
setCellConfig(x, y, config);
setColumnConfig(x, config);
setRowConfig(y, config);
setTableConfig(config);
For setters (setCellConfig
, setColumnConfig
, setRowConfig
) you may pass coordinate(s) for x
and y
:
- an integer
- string pattern that is a comma-separated list of indexes and/or ranges: '0..2,5' (from..to, to included)
Configuration options
interface ConfigValue {
align?: 'left' | 'center' | 'right';
bold?: boolean;
borderBottom?: boolean;
borderLeft?: boolean;
borderRight?: boolean;
borderTop?: boolean;
color?: 'black' | 'blue' | 'cyan' | 'default' | 'green' | 'magenta' | 'red' | 'white' | 'yellow';
height?: number;
italic?: boolean;
link?: string;
maxHeight?: number;
maxWidth?: number;
renderer?: (value: CellValue, x: number, y: number, config: ConfigValue, table: TableSnapshot) => string;
valign?: (typeof CONFIG_VALIGN)[number];
width?: number;
}
interface IncomingConfigValue extends ConfigValue {
border?: boolean;
horizontalBorder?: boolean;
verticalBorder?: boolean;
}
Preferences
Preferences for defaults can be set on both the Table static (ex: Table.setPreferences({ boldHeaders: true })
) or
for the instance (ex: const table = (new Table()).setPreferences({ verticalBorders: true })
).
You can check the current values by getting a copy of the preferences: table.getPreferences()
Default preferences
{
align: 'left',
boldHeaders: true,
columnHeaderAlign: 'left',
columnHeaderVAlign: 'bottom',
headerBorders: true,
horizontalBorders: false,
numberAlign: 'right',
rowHeaderAlign: 'right',
rowHeaderVAlign: 'top',
tableBorders: true,
valign: 'top',
verticalBorders: false
}
Output API
Output methods
table.toString(format?, options?);
table.toUTF8({ ansi: true, flavor: 'rounded', separatorSpaces: 3 });
table.toASCII({ ansi: true });
table.toMarkdown();
table.toCSV();
table.toTSV();
table.toHTML({ styles: true });
table.toJSON({ compact: true });
Default output format and options
If nothing is set, default format is utf8
Table.setOutputFormat(format, options?);
table.setOutputFormat(format, options?);
Custom builder output
Get pre-processed render data and use it with your own custom output formatter.
myCustomOutput(table.toRenderData());
The render data is a mash of the TableSnapshot and these additional interfaces:
interface TableSnapshot {
cellConfig: RenderConfig[][];
cellValue: CellValue[][];
columnConfig: ConfigValue[];
columnHeaders?: CellValue[];
columns: number;
rowConfig: ConfigValue[];
rowHeaders?: CellValue[];
rows: number;
startX: number;
startY: number;
tableConfig: ConfigValue;
}
interface TableRenderData extends TableSnapshot {
borders: TableRenderBorderData;
cellValueRendered: string[][] = [];
cellValueRenderedMultiline: string[][][] = [];
columnWidth: number[] = [];
rowHeight: number[] = [];
}
interface TableRenderBorderData {
readonly grid: (TLBRSeparator | string)[][][][] = [];
readonly horizontal: boolean[][] = [];
readonly horizontalSerparation: boolean[] = [];
readonly vertical: boolean[][] = [];
readonly verticalSerparation: boolean[] = [];
}