# 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.

Latest version **1.0.55** (published 2019-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install as-table
pnpm add as-table
yarn add as-table
bun add as-table
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.55 |
| Published | 2019-09-12 |
| First published | 2016-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 31.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 66 |
| Author | Vitaly Gordon |
| Maintainers | x84, xpl |
| Keywords | ASCII, table, sheet, grid, print, log, print table, object as table, array as table, text table, array table, object table, array format, columns, as table, tablefy, columns, stringify, print object, grid, tty, terminal, console, text, layout |

## Links

- npm: https://www.npmjs.com/package/as-table
- Repository: https://github.com/xpl/as-table
- Issues: https://github.com/xpl/as-table/issues
- npm.io page: https://npm.io/package/as-table

## Dependencies (1)

- [printable-characters](https://npm.io/package/printable-characters.md) ^1.0.42

## Alternatives

- [@lexical/table](https://npm.io/package/@lexical/table.md) — 3.0M weekly downloads
- [mantine-datatable](https://npm.io/package/mantine-datatable.md) — 98.2K weekly downloads
- [react-native-collapsible-tab-view](https://npm.io/package/react-native-collapsible-tab-view.md) — 70.6K weekly downloads
- [@handsontable/vue3](https://npm.io/package/@handsontable/vue3.md) — 16.1K weekly downloads
- [vuewordcloud](https://npm.io/package/vuewordcloud.md) — 7.2K weekly downloads

## Recent versions

- 1.0.55 (latest) — 2019-09-12
- 1.0.52 — 2019-06-23
- 1.0.51 — 2019-06-23
- 1.0.50 — 2019-06-22
- 1.0.49 — 2019-06-22
- 1.0.48 — 2019-06-22
- 1.0.47 — 2019-06-05
- 1.0.44 — 2019-05-31
- 1.0.41 — 2019-05-28
- 1.0.38 — 2019-03-29
- 1.0.37 — 2019-03-11
- 1.0.36 — 2018-07-16
- 1.0.33 — 2018-06-09
- 1.0.32 — 2018-04-29
- 1.0.31 — 2017-10-09
- … 29 more at https://npm.io/package/as-table/versions

## README

# as-table

[![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)

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`](https://github.com/xpl/printable-characters).

```bash
npm install as-table
```

## Printing objects

```javascript
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

```javascript
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

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

## Right align

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

## Providing a custom object printer

```javascript
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):

```javascript
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

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

asTable (data)
```

## Customizing the title rendering and the header separator

With string coloring by [`ansicolor`](https://github.com/xpl/ansicolor) (just for the demo purposes, any library will fit):

```javascript
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 } ])
```

<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">

---
_Source: https://npm.io/package/as-table · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
