Socket
Socket
Sign inDemoInstall

as-table

Package Overview
Dependencies
1
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    as-table

A simple function that print objects / arrays as ASCII tables. Handles ANSI styling and weird 💩 Unicode emoji symbols – they won't break the layout.


Version published
Weekly downloads
759K
decreased by-17.31%
Maintainers
2
Install size
220 kB
Created
Weekly downloads
 

Readme

Source

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

Keywords

FAQs

Last updated on 12 Sep 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc