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 |
|
5 | A simple function that print objects and arrays as ASCII tables. Supports ANSI styling (escape codes won't break the layout), thanks to [`printable-characters`](https://github.com/xpl/printable-characters).
|
6 |
|
7 | ```bash
|
8 | npm install as-table
|
9 | ```
|
10 |
|
11 | ## Printing objects
|
12 |
|
13 | ```javascript
|
14 | asTable = require ('as-table')
|
15 |
|
16 | asTable ([ { foo: true, string: 'abcde', num: 42 },
|
17 | { foo: false, string: 'qwertyuiop', num: 43 },
|
18 | { string: null, num: 44 } ])
|
19 | ```
|
20 | ```
|
21 | foo string num
|
22 | ----------------------
|
23 | true abcde 42
|
24 | false qwertyuiop 43
|
25 | null 44
|
26 | ```
|
27 |
|
28 | ## Printing arrays
|
29 |
|
30 | ```javascript
|
31 | asTable ([['qwe', '123456789', 'zxcvbnm'],
|
32 | ['qwerty', '12', 'zxcvb'],
|
33 | ['qwertyiop', '1234567', 'z']])
|
34 | ```
|
35 | ```
|
36 | qwe 123456789 zxcvbnm
|
37 | qwerty 12 zxcvb
|
38 | qwertyiop 1234567 z
|
39 | ```
|
40 |
|
41 | ## Limiting total width by proportionally trimming cells + setting columns delimiter
|
42 |
|
43 | ```javascript
|
44 | asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (data)
|
45 | ```
|
46 | ```
|
47 | qwe | 1234… | zxc…
|
48 | qwer… | 12 | zxc…
|
49 | qwer… | 1234… | z
|
50 | ```
|
51 |
|
52 | ## Providing custom object printer
|
53 |
|
54 | ```javascript
|
55 | asTable.configure ({ print: obj => (typeof obj === 'boolean') ? (obj ? 'yes' : 'no') : String (obj) }) (data)
|
56 | ```
|
57 | ```
|
58 | foo string num
|
59 | --------------------
|
60 | yes abcde 42
|
61 | no qwertyuiop 43
|
62 | null 44
|
63 | ```
|
64 |
|
65 | ## Obtaining pre-configured function
|
66 |
|
67 | ```javascript
|
68 | asTable = require ('as-table').configure ({ maxTotalWidth: 25, delimiter: ' | ' })
|
69 |
|
70 | asTable (data)
|
71 | ```
|
72 |
|
73 | ## Customizing title rendering and header separator
|
74 |
|
75 | With string coloring by [`ansicolor`](https://github.com/xpl/ansicolor) (just for the demo purposes, any library will fit):
|
76 |
|
77 | ```javascript
|
78 | asTable = require ('as-table').configure ({ title: x => x.bright, delimiter: ' | '.dim.cyan, dash: '-'.bright.cyan })
|
79 |
|
80 | console.log (
|
81 | asTable ([ { foo: true, string: 'abcde', num: 42 },
|
82 | { foo: false, string: 'qwertyuiop'.bgMagenta.green.bright, num: 43 } ])
|
83 | ```
|
84 |
|
85 | <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">
|