UNPKG

2.81 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 (escape codes 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## Providing custom object printer
53
54```javascript
55asTable.configure ({ print: obj => (typeof obj === 'boolean') ? (obj ? 'yes' : 'no') : String (obj) }) (data)
56```
57```
58foo string num
59--------------------
60yes abcde 42
61no qwertyuiop 43
62 null 44
63```
64
65## Obtaining pre-configured function
66
67```javascript
68asTable = require ('as-table').configure ({ maxTotalWidth: 25, delimiter: ' | ' })
69
70asTable (data)
71```
72
73## Customizing title rendering and header separator
74
75With string coloring by [`ansicolor`](https://github.com/xpl/ansicolor) (just for the demo purposes, any library will fit):
76
77```javascript
78asTable = require ('as-table').configure ({ title: x => x.bright, delimiter: ' | '.dim.cyan, dash: '-'.bright.cyan })
79
80console.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">