as-table
A simple function to print objects as ASCII tables
Last updated 8 years ago by xpl .
MIT · Original npm · Tarball · package.json
$ npm install as-table 
SYNC missed versions from official npm registry.

as-table

Build Status Coverage Status npm dependencies Status Scrutinizer Code Quality

A simple function that print objects and arrays as ASCII tables. Supports ANSI styling and weird 💩 Unicode emoji symbols (they won't break the layout), thanks to printable-characters.

npm install as-table

Printing objects

asTable = require ('as-table')

asTable ([ { foo: true,  string: 'abcde',      num: 42 },
           { foo: false, string: 'qwertyuiop', num: 43 },
           {             string:  null,        num: 44 } ])
foo    string      num
----------------------
true   abcde       42 
false  qwertyuiop  43 
       null        44 

Printing arrays

asTable ([['qwe',       '123456789', 'zxcvbnm'],
          ['qwerty',    '12',        'zxcvb'],
          ['qwertyiop', '1234567',   'z']])
qwe        123456789  zxcvbnm
qwerty     12         zxcvb
qwertyiop  1234567    z

Limiting total width by proportionally trimming cells + setting columns delimiter

asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (data)
qwe   | 1234… | zxc…
qwer… | 12    | zxc…
qwer… | 1234… | z   

Right align

asTable.configure ({ right: true }) (data)
      foo        bar      baz
-----------------------------
      qwe  123456789  zxcvbnm
   qwerty         12    zxcvb
qwertyiop    1234567        z

Providing a custom object printer

asTable.configure ({ print: x => (typeof x === 'boolean') ? (x ? 'yes' : 'no') : String (x) }) (data)
foo  string      num
--------------------
yes  abcde       42 
no   qwertyuiop  43 
     null        44 

The callback also receives a field name (in case of objects) or a column index (in case of arrays):

asTable = require ('as-table').configure ({
    print (x, k) {
        if (k === 'timestamp') return new Date (x).toGMTString()
        return String (x)
    }
})

asTable ([ { name: 'A', timestamp: 1561202591572 },
           { name: 'B', timestamp: 1558524240034 } ])

Obtaining a pre-configured function

asTable = require ('as-table').configure ({ maxTotalWidth: 25, delimiter: ' | ' })

asTable (data)

Customizing the title rendering and the header separator

With string coloring by ansicolor (just for the demo purposes, any library will fit):

asTable = require ('as-table').configure ({ title: x => x.bright, delimiter: ' | '.dim.cyan, dash: '-'.bright.cyan })

console.log (
   asTable ([ { foo: true,  string: 'abcde',                             num: 42 },
              { foo: false, string: 'qwertyuiop'.bgMagenta.green.bright, num: 43 } ])
screen shot 2017-07-21 at 23 46 14

Current Tags

  • 1.0.55                                ...           latest (5 years ago)

44 Versions

  • 1.0.55                                ...           5 years ago
  • 1.0.52                                ...           5 years ago
  • 1.0.51                                ...           5 years ago
  • 1.0.50                                ...           5 years ago
  • 1.0.49                                ...           5 years ago
  • 1.0.48                                ...           5 years ago
  • 1.0.47                                ...           5 years ago
  • 1.0.44                                ...           5 years ago
  • 1.0.41                                ...           5 years ago
  • 1.0.38                                ...           6 years ago
  • 1.0.37                                ...           6 years ago
  • 1.0.36                                ...           6 years ago
  • 1.0.33                                ...           6 years ago
  • 1.0.32                                ...           7 years ago
  • 1.0.31                                ...           7 years ago
  • 1.0.30                                ...           7 years ago
  • 1.0.29                                ...           7 years ago
  • 1.0.28                                ...           7 years ago
  • 1.0.27                                ...           7 years ago
  • 1.0.26                                ...           7 years ago
  • 1.0.25                                ...           7 years ago
  • 1.0.24                                ...           7 years ago
  • 1.0.23                                ...           7 years ago
  • 1.0.22                                ...           7 years ago
  • 1.0.21                                ...           7 years ago
  • 1.0.20                                ...           7 years ago
  • 1.0.17                                ...           7 years ago
  • 1.0.16                                ...           7 years ago
  • 1.0.15                                ...           7 years ago
  • 1.0.14                                ...           7 years ago
  • 1.0.13                                ...           7 years ago
  • 1.0.12                                ...           8 years ago
  • 1.0.11                                ...           8 years ago
  • 1.0.10                                ...           8 years ago
  • 1.0.9                                ...           8 years ago
  • 1.0.8                                ...           8 years ago
  • 1.0.7                                ...           8 years ago
  • 1.0.6                                ...           8 years ago
  • 1.0.5                                ...           8 years ago
  • 1.0.4                                ...           8 years ago
  • 1.0.3                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (2)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE