npm.io
1.0.0 • Published 1 month ago

@atombrenner/text-table

Licence
GPL-3.0-or-later
Version
1.0.0
Deps
0
Size
47 kB
Vulns
0
Weekly
5

@atombrenner/text-table

Render text tables for display with monospaced fonts (terminal, <pre> tags).

Fruits  | Ratio |  Price
--------|-------|-------
Apples  |  0.38 |  23.13
Bananas |  0.04 |  41.09
Pears   |  0.58 |  45.34
--------|-------|-------
Total   |  1.00 | 109.56

Everything has sensible defaults to give you a pretty text table without configuration:

  • minimal bordering
  • autodetect column type (string or number)
  • right align number columns
  • format numbers with two decimal digits

For more advanced use cases you can

  • provide a header with column names
  • specify a max or fixed width per column
  • specify alignment per column
  • specify percent or custom units
  • display a footer
  • add borders
  • provide custom theme
  • provide custom formatting per column

This package has zero dependencies.

Usage Examples

import { textTable, textRight, number } from '@atombrenner/text-table'

const data = [
  ['Apples', 0.375, 23.13005],
  ['Bananas', 0.04246, 41.09001],
  ['Pears', 0.58254, 45.34],
]
console.log(textTable(data))
Apples  | 37.50 | 23.13
Bananas |  4.25 | 41.09
Pears   | 58.25 | 45.34
  • textTable(data, ['Fruits', 'Ratio', 'Price'])
Fruits  | Ratio | Price
--------|-------|------
Apples  |  0.38 | 23.13
Bananas |  0.04 | 41.09
Pears   |  0.58 | 45.34
  • textTable(data, [textRight('Fruits'), number('Ratio', 3)])
 Fruits | Ratio
--------|------
 Apples | 0.375
Bananas | 0.042
  Pears | 0.583
  • textTable(data, ['Fruits', percent('Percent', 3)])
Fruits  | Percent
--------|--------
Apples  |   38.5%
Bananas |    4.3%
Pears   |   58.3%

Complex Options

import { textTable, percent, unit, theme }

const footer =  [ 'Total', 1, 109.56006 ]

console.log(
  textTable([...data, footer], {
    columns: ['Fruits', percent('Percent'), unit('Price', 'EUR/kg')],
    footer: true,
    border: true,
    theme: theme.lightLine,
  }),
)
┌─────────┬─────────┬───────────────┐
│ Fruits  │ Percent │         Price │
├─────────┼─────────┼───────────────┤
│ Apples  │   37.5% │  23.13 EUR/kg │
│ Bananas │    4.2% │  41.09 EUR/kg │
│ Pears   │   58.3% │  45.34 EUR/kg │
├─────────┼─────────┼───────────────┤
│ Total   │  100.0% │ 109.56 EUR/kg │
└─────────┴─────────┴───────────────┘
theme.lightLine              theme.doubleLine              theme.heavyLine
┌─────────┬───────┬───────┐  ╔═════════╦════════╦═══════╗  ┏━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
│ Fruits  │ Ratio │ Price │  ║ Fruits  ║    Max ║ Price ║  ┃ Fruits  ┃  Ratio ┃ Price ┃
├─────────┼───────┼───────┤  ╠═════════╬════════╬═══════╣  ┣━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
│ Apples  │   38% │ 33.13 │  ║ Apples  ║    38% ║ 33.13 ║  ┃ Apples  ┃    38% ┃ 33.13 ┃
│ Bananas │    4% │  4.09 │  ║ Bananas ║     4% ║  4.09 ║  ┃ Bananas ┃     4% ┃  4.09 ┃
│ Pears   │   58% │ 45.34 │  ║ Pears   ║    58% ║ 45.34 ║  ┃ Pears   ┃    58% ┃ 45.34 ┃
├─────────┼───────┼───────┤  ╠═════════╬════════╬═══════╣  ┣━━━━━━━━━╋━━━━━━━━╋━━━━━━━┫
│ Total   │  100% │ 82.56 │  ║ Total   ║ 100.00 ║ 82.56 ║  ┃ Total   ┃   100% ┃ 82.56 ┃
└─────────┴───────┴───────┘  ╚═════════╩════════╩═══════╝  ┗━━━━━━━━━┻━━━━━━━━┻━━━━━━━┛

You can provide a custom theme by setting a template string if you don't like one of the predefined themes. A template string defines the characters for border drawing.

Reference

Overloads:

  • textTable(data)
  • textTable(data, columns)
  • textTable(data, columns, options)
  • textTable(data, options)

Gotchas

Publishing

npm version <major|minor|patch> npm login npm publish --ignore-scripts