UNPKG

3.09 kBMarkdownView Raw
1# as-table
2
3[![Build Status](https://travis-ci.org/xpl/as-table.svg?branch=master)](https://travis-ci.org/xpl/as-table) [![Coverage Status](https://coveralls.io/repos/github/xpl/as-table/badge.svg)](https://coveralls.io/github/xpl/as-table) [![npm](https://img.shields.io/npm/v/as-table.svg)](https://npmjs.com/package/as-table) [![dependencies Status](https://david-dm.org/xpl/as-table/status.svg)](https://david-dm.org/xpl/as-table) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/xpl/as-table.svg)](https://scrutinizer-ci.com/g/xpl/as-table/?branch=master)
4
5A 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`](https://github.com/xpl/printable-characters).
6
7```bash
8npm install as-table
9```
10
11## Printing objects
12
13```javascript
14asTable = require ('as-table')
15
16asTable ([ { foo: true, string: 'abcde', num: 42 },
17 { foo: false, string: 'qwertyuiop', num: 43 },
18 { string: null, num: 44 } ])
19```
20```
21foo string num
22----------------------
23true abcde 42
24false qwertyuiop 43
25 null 44
26```
27
28## Printing arrays
29
30```javascript
31asTable ([['qwe', '123456789', 'zxcvbnm'],
32 ['qwerty', '12', 'zxcvb'],
33 ['qwertyiop', '1234567', 'z']])
34```
35```
36qwe 123456789 zxcvbnm
37qwerty 12 zxcvb
38qwertyiop 1234567 z
39```
40
41## Limiting total width by proportionally trimming cells + setting columns delimiter
42
43```javascript
44asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (data)
45```
46```
47qwe | 1234… | zxc…
48qwer… | 12 | zxc…
49qwer… | 1234… | z
50```
51
52## Right align
53
54```javascript
55asTable.configure ({ right: true }) (data)
56```
57```
58 foo bar baz
59-----------------------------
60 qwe 123456789 zxcvbnm
61 qwerty 12 zxcvb
62qwertyiop 1234567 z
63```
64
65## Providing a custom object printer
66
67```javascript
68asTable.configure ({ print: obj => (typeof obj === 'boolean') ? (obj ? 'yes' : 'no') : String (obj) }) (data)
69```
70```
71foo string num
72--------------------
73yes abcde 42
74no qwertyuiop 43
75 null 44
76```
77
78## Obtaining a pre-configured function
79
80```javascript
81asTable = require ('as-table').configure ({ maxTotalWidth: 25, delimiter: ' | ' })
82
83asTable (data)
84```
85
86## Customizing the title rendering and the header separator
87
88With string coloring by [`ansicolor`](https://github.com/xpl/ansicolor) (just for the demo purposes, any library will fit):
89
90```javascript
91asTable = require ('as-table').configure ({ title: x => x.bright, delimiter: ' | '.dim.cyan, dash: '-'.bright.cyan })
92
93console.log (
94 asTable ([ { foo: true, string: 'abcde', num: 42 },
95 { foo: false, string: 'qwertyuiop'.bgMagenta.green.bright, num: 43 } ])
96```
97
98<img width="179" alt="screen shot 2017-07-21 at 23 46 14" src="https://user-images.githubusercontent.com/1707/28481945-dcb0f8d6-6e6e-11e7-896e-dfad40662daf.png">
99