# nice-table

> A non-overflowing `console.table` alternative with customization options.

Latest version **1.1.0** (published 2022-07-17) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-07-17 |
| First published | 2022-07-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 31.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tim van Dam |
| Maintainers | timvd |
| Keywords | table, console.table |

## Links

- npm: https://www.npmjs.com/package/nice-table
- Repository: https://github.com/timvandam/nice-table
- Homepage: https://github.com/timvandam/nice-table#readme
- Issues: https://github.com/timvandam/nice-table/issues
- npm.io page: https://npm.io/package/nice-table

## Dependencies (2)

- [wrap-ansi](https://npm.io/package/wrap-ansi.md) ^7.0.0
- [string-width](https://npm.io/package/string-width.md) ^4.2.3

## 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.1.0 (latest) — 2022-07-17
- 1.0.1 — 2022-07-16

## README

# nice-table
A non-overflowing `console.table` alternative with customization options.

## Usage
```ts
import { createTable } from 'nice-table';

type Person = {
    name: string;
    age: number;
};

const myData: Person[] = [
    { name: 'John', age: 30 },
    { name: 'Jane', age: 25 },
    { name: 'Joe', age: 20 },
    {
        name: 'Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso',
        age: new Date(Date.now() - Date.UTC(1881, 3, 8)).getUTCFullYear() - 1970,
    },
];

console.log(
    createTable<Person>(myData, ['name', 'age'], {
        maxWidth: 60,
        columnSizing: 'stretch',
        horizontalAlignment: 'middle',
        verticalAlignment: 'middle',
        fullWidth: true,
        indexColumn: false,
        throwIfTooSmall: false,
    }),
);

// Output:
// ┌────────────────────────────────────────────────────┬─────┐
// │                        name                        │ age │
// ├────────────────────────────────────────────────────┼─────┤
// │                        John                        │ 30  │
// │                        Jane                        │ 25  │
// │                        Joe                         │ 20  │
// │      Pablo Diego José Francisco de Paula Juan      │     │
// │  Nepomuceno María de los Remedios Cipriano de la   │ 141 │
// │         Santísima Trinidad Ruiz y Picasso          │     │
// └────────────────────────────────────────────────────┴─────┘
```

### Colored Output
```ts
import * as util from 'node:util';

// ...

console.log(
    createTable<Person>(myData, ['name', 'age'], {
        stringify: (value: unknown) => util.inspect(value, { colors: true }),
    }),
);
```

## Options
### `maxWidth`
The maximum width of the table.
This width will never be exceeded by the table.
Can be set to `process.stdout.columns` to use the terminal width in Node.js.

Defaults to `80`.

### `columnSizing`
The strategy used to determine the width of each column.
There are two possible values:
- `'stretch'`: The size of each column is proportional to the length of its content. All columns larger than `maxWidth / columnCount` are shrunk by the same ratio in case the table is too large for the configured `maxWidth`.    
- `'even'`: All columns will have the same size.

Defaults to `'stretch'`.

### `horizontalAlignment`
The horizontal alignment the text in all table cells. Possible values are `'left'`, `'middle'`, `'right'`.

Defaults to `'middle'`.

### `verticalAlignment`
The vertical alignment the text in all table cells. Possible values are `'top'`, `'middle'`, `'bottom'`.

Defaults to `'middle'`.

### `fullWidth`
Whether to stretch the table to the `maxWidth`.

Defaults to `false`.

### `throwIfTooSmall`
Whether to throw an error if the `maxWidth` is too small to fit the content.
`maxWidth` should be at least `4 * columnCount + 1` to fit a table with `columnCount` columns.

If set to `false` will return a message indicating that there is not enough space to fit the table.

Defaults to `true`.

### `indexColumn`
Whether to include an index column.

Defaults to `false`.

### `stringify`
A function converts values into strings before they are is displayed.
ANSI codes are supported, so colored input can be achieved as shown in [Colored Output](#colored-output).

Defaults to `String`.

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