Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Table<TRow, TDColumns>

Represent a dataset in tabular form. The columns are infered from the dataset's first item.

Type Parameters

  • TRow extends Row

    Type of a dataset row.

  • TDColumns extends object = never

    Type of the dynamic columns.

Hierarchy

  • Table

Index

Constructors

  • new Table<TRow, TDColumns>(dataset: TRow[], config?: Partial<{ align: "LEFT" | "CENTER" | "RIGHT"; bgColorColumns: string[]; body: Partial<{ accumulation: Partial<{ bgColor: string; columns: Partial<Accumulation<TRow, TDColumns>>; separator: string }>; bgColor: string; fillEmpty: Partial<FillEmptyOption<DatasetRow<TRow, TDColumns>>>; highlightCell: Partial<{ bold: boolean; italic: boolean; textColor: string; underline: boolean; func: any }>; highlightRow: Partial<{ bgColor: string; func: any }>; peek: number | [number, number]; precision: number; striped: boolean; subset: [number?, number?]; textColor: string; filterRow: any }>; border: Partial<{ color: string; groupSize: number; horizontal: string; vertical: string }>; header: Partial<{ bgColor: string; bold: boolean; displayNames: Partial<{ [ key in number | "#"]: string }>; dynamic: DynamicColumn<TRow, TDColumns>; exclude: InferRowAttributes<TRow>[]; include: InferRowAttributes<TRow>[]; italic: boolean; lowercase: boolean; maxWidth: number | "auto"; order: InferAttributesOrigin<TRow, TDColumns>[]; origin: boolean; separator: string; textColor: string; underline: boolean; uppercase: boolean; upperfirst: boolean; width: number | "auto" | "stretch" }>; padding: Partial<{ char: string; size: number }>; sort: Sort<InferAttributesOrigin<TRow, TDColumns>> }>): Table<TRow, TDColumns>
  • Creates a new Table instance.

    Type Parameters

    • TRow extends Row

    • TDColumns extends object = never

    Parameters

    • dataset: TRow[]

      the dataset

    • config: Partial<{ align: "LEFT" | "CENTER" | "RIGHT"; bgColorColumns: string[]; body: Partial<{ accumulation: Partial<{ bgColor: string; columns: Partial<Accumulation<TRow, TDColumns>>; separator: string }>; bgColor: string; fillEmpty: Partial<FillEmptyOption<DatasetRow<TRow, TDColumns>>>; highlightCell: Partial<{ bold: boolean; italic: boolean; textColor: string; underline: boolean; func: any }>; highlightRow: Partial<{ bgColor: string; func: any }>; peek: number | [number, number]; precision: number; striped: boolean; subset: [number?, number?]; textColor: string; filterRow: any }>; border: Partial<{ color: string; groupSize: number; horizontal: string; vertical: string }>; header: Partial<{ bgColor: string; bold: boolean; displayNames: Partial<{ [ key in number | "#"]: string }>; dynamic: DynamicColumn<TRow, TDColumns>; exclude: InferRowAttributes<TRow>[]; include: InferRowAttributes<TRow>[]; italic: boolean; lowercase: boolean; maxWidth: number | "auto"; order: InferAttributesOrigin<TRow, TDColumns>[]; origin: boolean; separator: string; textColor: string; underline: boolean; uppercase: boolean; upperfirst: boolean; width: number | "auto" | "stretch" }>; padding: Partial<{ char: string; size: number }>; sort: Sort<InferAttributesOrigin<TRow, TDColumns>> }> = {}

      the config

    Returns Table<TRow, TDColumns>

Properties

_config: Required<Partial<{ align: "LEFT" | "CENTER" | "RIGHT"; bgColorColumns: string[]; body: Partial<{ accumulation: Partial<{ bgColor: string; columns: Partial<Accumulation<TRow, TDColumns>>; separator: string }>; bgColor: string; fillEmpty: Partial<FillEmptyOption<DatasetRow<TRow, TDColumns>>>; highlightCell: Partial<{ bold: boolean; italic: boolean; textColor: string; underline: boolean; func: any }>; highlightRow: Partial<{ bgColor: string; func: any }>; peek: number | [number, number]; precision: number; striped: boolean; subset: [number?, number?]; textColor: string; filterRow: any }>; border: Partial<{ color: string; groupSize: number; horizontal: string; vertical: string }>; header: Partial<{ bgColor: string; bold: boolean; displayNames: Partial<{ [ key in number | "#"]: string }>; dynamic: DynamicColumn<TRow, TDColumns>; exclude: InferRowAttributes<TRow>[]; include: InferRowAttributes<TRow>[]; italic: boolean; lowercase: boolean; maxWidth: number | "auto"; order: InferAttributesOrigin<TRow, TDColumns>[]; origin: boolean; separator: string; textColor: string; underline: boolean; uppercase: boolean; upperfirst: boolean; width: number | "auto" | "stretch" }>; padding: Partial<{ char: string; size: number }>; sort: Sort<InferAttributesOrigin<TRow, TDColumns>> }>>

The table config.

_dataset: DatasetRow<TRow, TDColumns>[]

The dataset.

accumulatedRow: AccumulationRow<TRow, TDColumns>

The row containing the accumulated data.

columnNames: InferDatasetRowAttributesOrigin<TRow, TDColumns>[] = []

The column names. Including the dynamic and origin column names

columnWidths: Map<InferDatasetRowAttributesOrigin<TRow, TDColumns>, number> = ...

The maximum width of each column.

tableWidth: number

The table width.

touched: boolean = true

A flag to check whether the header or body has changed since the last build. Prevents unnecessary builds.

Accessors

  • get columns(): InferDatasetRowAttributesOrigin<TRow, TDColumns>[]
  • Returns InferDatasetRowAttributesOrigin<TRow, TDColumns>[]

  • get config(): Required<Partial<{ align: "LEFT" | "CENTER" | "RIGHT"; bgColorColumns: string[]; body: Partial<{ accumulation: Partial<{ bgColor: string; columns: Partial<Accumulation<TRow, TDColumns>>; separator: string }>; bgColor: string; fillEmpty: Partial<FillEmptyOption<DatasetRow<TRow, TDColumns>>>; highlightCell: Partial<{ bold: boolean; italic: boolean; textColor: string; underline: boolean; func: any }>; highlightRow: Partial<{ bgColor: string; func: any }>; peek: number | [number, number]; precision: number; striped: boolean; subset: [number?, number?]; textColor: string; filterRow: any }>; border: Partial<{ color: string; groupSize: number; horizontal: string; vertical: string }>; header: Partial<{ bgColor: string; bold: boolean; displayNames: Partial<{ [ key in number | "#"]: string }>; dynamic: DynamicColumn<TRow, TDColumns>; exclude: InferRowAttributes<TRow>[]; include: InferRowAttributes<TRow>[]; italic: boolean; lowercase: boolean; maxWidth: number | "auto"; order: InferAttributesOrigin<TRow, TDColumns>[]; origin: boolean; separator: string; textColor: string; underline: boolean; uppercase: boolean; upperfirst: boolean; width: number | "auto" | "stretch" }>; padding: Partial<{ char: string; size: number }>; sort: Sort<InferAttributesOrigin<TRow, TDColumns>> }>>
  • Returns Required<Partial<{ align: "LEFT" | "CENTER" | "RIGHT"; bgColorColumns: string[]; body: Partial<{ accumulation: Partial<{ bgColor: string; columns: Partial<Accumulation<TRow, TDColumns>>; separator: string }>; bgColor: string; fillEmpty: Partial<FillEmptyOption<DatasetRow<TRow, TDColumns>>>; highlightCell: Partial<{ bold: boolean; italic: boolean; textColor: string; underline: boolean; func: any }>; highlightRow: Partial<{ bgColor: string; func: any }>; peek: number | [number, number]; precision: number; striped: boolean; subset: [number?, number?]; textColor: string; filterRow: any }>; border: Partial<{ color: string; groupSize: number; horizontal: string; vertical: string }>; header: Partial<{ bgColor: string; bold: boolean; displayNames: Partial<{ [ key in number | "#"]: string }>; dynamic: DynamicColumn<TRow, TDColumns>; exclude: InferRowAttributes<TRow>[]; include: InferRowAttributes<TRow>[]; italic: boolean; lowercase: boolean; maxWidth: number | "auto"; order: InferAttributesOrigin<TRow, TDColumns>[]; origin: boolean; separator: string; textColor: string; underline: boolean; uppercase: boolean; upperfirst: boolean; width: number | "auto" | "stretch" }>; padding: Partial<{ char: string; size: number }>; sort: Sort<InferAttributesOrigin<TRow, TDColumns>> }>>

  • get dataset(): DatasetRow<TRow, TDColumns>[]
  • set dataset(dataset: DatasetRow<TRow, TDColumns>[]): void
  • Returns DatasetRow<TRow, TDColumns>[]

  • Parameters

    • dataset: DatasetRow<TRow, TDColumns>[]

    Returns void

  • get shape(): [rows: number, columns: number]
  • Returns [rows: number, columns: number]

Methods

  • build(force?: boolean): void
  • Builds the table. For performance reasons the table is only built if Table.touched is true.

    Parameters

    • force: boolean = false

      force the build

    Returns void

  • buildBody(): string
  • Builds the body.

    Returns string

    the build body

  • buildBodyCell(row: number, col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): [content: string, overflow: string]
  • Builds the given body cell content.

    Parameters

    • row: number

      the cell's row

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    Returns [content: string, overflow: string]

    the built cell content

  • buildBodyPeekRow(): string
  • Builds the peek row. The placeholder row for the hidden rows if the peek config is set.

    Returns string

    the peek row

  • buildBodyRow(row: number): string
  • Builds the given body row.

    Parameters

    • row: number

      the row

    Returns string

    the built row content

  • buildBodyRowHorizontalBorder(row: number): string
  • Builds the horizontal border for the given row.

    Parameters

    • row: number

      the row

    Returns string

    the horizontal row border

  • buildCellContent(padLeft: number, text: string, padRight: number): CellContent
  • Builds a cell content array.

    Parameters

    • padLeft: number

      the cell's left padding

    • text: string

      the cell's text

    • padRight: number

      the cell's right padding

    Returns CellContent

    the cell content

  • buildColumnNames(): void
  • Builds the column names from the dataset in the right order. The result is stored in Table.columnNames.

    All column names are converted to string in order to avoid complications when using arrays and numbers and indices.

    Returns void

  • buildDataset(dataset: TRow[]): DatasetRow<TRow, TDColumns>[]
  • Merges the user dataset with the dynamic columns.

    Parameters

    • dataset: TRow[]

    Returns DatasetRow<TRow, TDColumns>[]

    the new dataset

  • buildEmptyCellContent(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): CellContent
  • Builds an empty cell content.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    Returns CellContent

    the empty cell content

  • buildHeader(): string
  • Builds the header.

    Returns string

    the built header

  • buildHeaderCell(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): [content: string, overflow: string]
  • Builds the given header cell content.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    Returns [content: string, overflow: string]

    the built cell content

  • buildRowOverflow(row: number, overflow: string[]): string
  • Builds the subsequent lines (overflow) of the given row. Set row=0 for the header.

    Parameters

    • row: number

      the initial row

    • overflow: string[]

      the text overflow for each solumn

    Returns string

    the subsequent lines

  • buildRowSeparator(separator: string): string
  • Builds the row separator.

    Parameters

    • separator: string

      the separator character

    Returns string

    the row separator string

  • calculateAccumulation(): AccumulationRow<TRow, TDColumns>
  • Computes the rows of the accumulated columns.

    Returns AccumulationRow<TRow, TDColumns>

    the calculated rows

  • calculateBodyCellPadding(row: number, col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): number
  • Calculates the body cell padding.

    Parameters

    • row: number

      the cell's row

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    Returns number

    the cell padding

  • calculateColumnWidths(): void
  • calculateHeaderCellPadding(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): number
  • Calculates the header cell padding. The padding is based on the column's width and its display name.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    Returns number

    the cell padding

  • calculateTableWidth(): number
  • Calculates the width of the complete table. The table width is based on the width of the columns, the padding and the border.

    Returns number

    the table width

  • exportFile(filepath: string, mode?: OpenMode): void
  • Exports the plain table to the given file (without advanced style).

    Parameters

    • filepath: string

      the filepath

    • mode: OpenMode = 'w'

      the file's open mode

    Returns void

  • formatBodyCellContent(row: number, col: InferDatasetRowAttributesOrigin<TRow, TDColumns>, content: CellContent): string
  • Formats the content of the given body cell.

    Parameters

    • row: number

      the cell's row

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    • content: CellContent

      the cell's content

    Returns string

    the formatted cell content

  • formatBodyRowContent(row: number, content: string): string
  • Formats the content of the given body row.

    Parameters

    • row: number

      the cell's row

    • content: string

      the rows's content

    Returns string

    the formatted row content

  • formatHeaderCellContent(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>, content: CellContent): string
  • Formats the content of the given header cell. The return value includes also the content's length because the content also contains ANSI escape codes.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      cell's column

    • content: CellContent

      cell's content

    Returns string

    the formatted cell content and its length

  • getCellText(row: number, col: InferDatasetRowAttributesOrigin<TRow, TDColumns>, cropped?: boolean): string
  • Gets the text of given the given cell.

    Parameters

    • row: number

      the cell's row

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's column

    • cropped: boolean = true

      whether the text should be cropped or not.

    Returns string

    the cell text

  • getColumnDisplayName(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>, cropped?: boolean): string
  • Gets the display name of the given column.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the column

    • cropped: boolean = false

      whether the column name should be cropped or not.

    Returns string

    the column's display name string

  • getColumnIndex(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): number
  • Gets the index of the given column.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the column's name

    Returns number

    the column's index

  • getColumnWidth(col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): number
  • Gets the width of the given column.

    Parameters

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the column

    Returns number

    the column's text width

  • getConsoleWidth(): number
  • Get the width of the console window. Padding is substracted from the width.

    Returns number

    the console width

  • getDataCell(row: number, col: InferDatasetRowAttributesOrigin<TRow, TDColumns>): AccumulationRow<TRow, TDColumns>[InferDatasetRowAttributesOrigin<TRow, TDColumns>]
  • Gets the value of the given cell in the dataset.

    Parameters

    • row: number

      the cell's row

    • col: InferDatasetRowAttributesOrigin<TRow, TDColumns>

      the cell's col

    Returns AccumulationRow<TRow, TDColumns>[InferDatasetRowAttributesOrigin<TRow, TDColumns>]

    the cell's value

  • getPadding(size: number): string
  • Gets the character padding of the given size.

    Parameters

    • size: number

      the padding size

    Returns string

    the character padding

  • isBorder(text: string): boolean
  • Checks whether the given text is a border character.

    Parameters

    • text: string

      the text to check

    Returns boolean

    whether the text is a border character

  • parseCellText(text: unknown): string
  • Parses the given cell text to String.

    Parameters

    • text: unknown

      the text to parse

    Returns string

    the parsed cell text

  • print(clear?: boolean): void
  • Prints the table to the console.

    Parameters

    • clear: boolean = false

      clear the console before printing

    Returns void

  • printPlain(): void
  • Prints the plain (unstyled) table to the console.

    Returns void

  • sort(): void
  • toPlainString(): string
  • Gets the table as plain string without any advanced styling. Can be used for example to write the table to a file or to paste it anywhere as text.

    Returns string

    the plain table string

  • toString(): string
  • Gets the table as string. Can be used to print the table on the console.

    Returns string

    the table string

Generated using TypeDoc