ascii-table vs cli-table vs console-table-printer vs console.table vs table
Node.js Table Formatting Libraries
ascii-tablecli-tableconsole-table-printerconsole.tabletableSimilar Packages:

Node.js Table Formatting Libraries

These libraries are designed to help developers create and display tables in the console, making it easier to visualize data in a structured format. They cater to different needs and preferences, providing various features for formatting, styling, and printing tables in Node.js applications. Each library has its unique approach and capabilities, allowing developers to choose based on their specific requirements for console output.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
ascii-table0181-2310 years agoMIT
cli-table02,29417.3 kB185 years ago-
console-table-printer022851.5 kB163 months agoMIT
console.table0139-59 years agoMIT
table0973335 kB332 years agoBSD-3-Clause

Feature Comparison: ascii-table vs cli-table vs console-table-printer vs console.table vs table

Ease of Use

  • ascii-table:

    ascii-table is designed for simplicity, allowing developers to create tables with minimal code. Its straightforward API makes it easy to integrate into existing projects without a steep learning curve.

  • cli-table:

    cli-table offers a more feature-rich API, which may require a bit more setup but provides greater flexibility in table formatting. It is still relatively easy to use for those familiar with Node.js.

  • console-table-printer:

    console-table-printer emphasizes ease of use with a clean API that allows for quick table printing. It is user-friendly and suitable for developers looking for a hassle-free solution.

  • console.table:

    console.table is the simplest option, as it is built into Node.js. Developers can use it without any additional installation, making it the quickest way to display data in a table format.

  • table:

    table strikes a balance between ease of use and functionality, providing a straightforward API while still allowing for customization.

Customization

  • ascii-table:

    ascii-table offers limited customization options, focusing on basic ASCII table formatting. It is best for simple use cases where advanced styling is not required.

  • cli-table:

    cli-table excels in customization, allowing developers to define column widths, alignments, and styles. This makes it ideal for applications that need a polished output.

  • console-table-printer:

    console-table-printer provides some customization options, including styling and alignment, but is not as extensive as cli-table. It is suitable for most common use cases.

  • console.table:

    console.table has no customization options; it outputs tables in a default format. It is best for quick debugging rather than polished presentation.

  • table:

    table offers a moderate level of customization, allowing developers to adjust the appearance of tables while keeping the API simple.

Performance

  • ascii-table:

    ascii-table is lightweight and performs well for small to medium datasets. Its simplicity contributes to faster rendering times in console applications.

  • cli-table:

    cli-table may have slightly slower performance due to its advanced features, but it is still efficient for most applications. Performance is generally acceptable for typical use cases.

  • console-table-printer:

    console-table-printer is optimized for performance, making it suitable for printing tables quickly without significant overhead.

  • console.table:

    console.table is highly performant as it is part of the Node.js core, making it very efficient for logging data without additional dependencies.

  • table:

    table is designed to handle moderate amounts of data efficiently, providing a good balance between performance and functionality.

Data Handling

  • ascii-table:

    ascii-table is best suited for simple data structures, such as arrays of objects. It does not support complex nested data well.

  • cli-table:

    cli-table can handle more complex data structures, including multi-line cells and various data types, making it versatile for different applications.

  • console-table-printer:

    console-table-printer supports various data types and can handle arrays and objects effectively, making it suitable for diverse datasets.

  • console.table:

    console.table works well with arrays and objects, providing a straightforward way to log structured data in a tabular format.

  • table:

    table is capable of handling complex data structures, allowing for more intricate representations of data in tables.

Community and Support

  • ascii-table:

    ascii-table has a smaller community and limited support, which may affect the availability of resources and examples.

  • cli-table:

    cli-table has a larger community and more resources available, making it easier to find help and examples online.

  • console-table-printer:

    console-table-printer has a growing community, and while resources are available, they may not be as extensive as those for cli-table.

  • console.table:

    console.table benefits from being part of Node.js, ensuring robust support and documentation due to its built-in nature.

  • table:

    table has a moderate community presence, providing a decent amount of resources and support for users.

How to Choose: ascii-table vs cli-table vs console-table-printer vs console.table vs table

  • ascii-table:

    Choose ascii-table if you need a simple and lightweight solution for displaying ASCII tables in the console. It is easy to use and requires minimal setup, making it ideal for quick debugging or displaying small datasets.

  • cli-table:

    Select cli-table if you require more advanced features such as column alignment, custom styles, and support for multi-line cells. It is well-suited for applications that need to present data in a more visually appealing way, with a focus on customization.

  • console-table-printer:

    Opt for console-table-printer if you want a library that provides a straightforward API for printing tables with a focus on ease of use. It supports various data types and offers built-in styling options, making it a good choice for quick implementations.

  • console.table:

    Use console.table if you are looking for a built-in solution provided by Node.js that allows you to log arrays or objects in a tabular format directly to the console. This is the simplest option for quick debugging without any additional dependencies.

  • table:

    Choose table if you need a library that offers a balance between simplicity and functionality, with support for complex data structures and customizable output. It is suitable for developers who want more control over the table's appearance without overwhelming complexity.

README for ascii-table

Ascii Table

Build Status devDependency Status NPM version

Easy table output for node debugging, but you could probably do more with it, since its just a string.

Table of Contents

Usage

Node.js

var AsciiTable = require('ascii-table')

Browser

<script src="ascii-table.min.js"></script>

Note: If using in the browser, it will be placed under window.AsciiTable

Example

Basic usage

var table = new AsciiTable('A Title')
table
  .setHeading('', 'Name', 'Age')
  .addRow(1, 'Bob', 52)
  .addRow(2, 'John', 34)
  .addRow(3, 'Jim', 83)

console.log(table.toString())
.----------------.
|    A Title     |
|----------------|
|   | Name | Age |
|---|------|-----|
| 1 | Bob  |  52 |
| 2 | John |  34 |
| 3 | Jim  |  83 |
'----------------'

We can make a simple table without a title or headings as well.

var table = new AsciiTable()

table
  .addRow('a', 'apple', 'Some longer string')
  .addRow('b', 'banana', 'hi')
  .addRow('c', 'carrot', 'meow')
  .addRow('e', 'elephants')


console.log(table.toString())
.------------------------------------.
| a | apple     | Some longer string |
| b | banana    | hi                 |
| c | carrot    | meow               |
| e | elephants |                    |
'------------------------------------'

API

Static Methods

AsciiTable

See: AsciiTable.factory for details on instantiation

AsciiTable.factory([title], [options])

Table instance creator

  • title - table title (optional, default null)
  • options - table options (optional)
    • prefix - string prefix to add to each line on render

Note: If an object is passed in place of the title, the fromJSON method will be used to populate the table.

Example:

var table = AsciiTable.factory('title')

var table = AsciiTable.factory({
  title: 'Title'
, heading: [ 'id', 'name' ]
, rows: [ 
    [ 1, 'Bob' ]
  , [ 2, 'Steve' ] 
  ] 
})

AsciiTable.align(direction, val, len, [pad])

Shortcut to one of the three following methods

  • direction - alignment direction (AsciiTable.LEFT, AsciiTable.CENTER, AsciiTable.RIGHT)
  • val - string to align
  • len - total length of created string
  • pad - padding / fill char (optional, default ' ')

Example:

table.align(AsciiTable.LEFT, 'hey', 7) // 'hey    '

AsciiTable.alignLeft(val, len, [pad])

  • val - string to align
  • len - total length of created string
  • pad - padding / fill char (optional, default ' ')

Example:

table.alignLeft('hey', 7, '-') // 'hey----'

AsciiTable.alignCenter(val, len, [pad])

  • val - string to align
  • len - total length of created string
  • pad - padding / fill char (optional, default ' ')

Example:

table.alignCenter('hey', 7) // '  hey  '

AsciiTable.alignRight(val, len, [pad])

  • val - string to align
  • len - total length of created string
  • pad - padding / fill char (optional, default ' ')

Example:

table.alignRight('hey', 7) // '    hey'

AsciiTable.alignAuto(val, len, [pad])

Attempt to do intelligent alignment of provided val, String input will be left aligned, Number types will be right aligned.

  • val - string to align
  • len - total length of created string
  • pad - padding / fill char (optional, default ' ')

Example:

table.align(AsciiTable.LEFT, 'hey', 7) // 'hey    '

AsciiTable.arrayFill(len, [val])

Create a new array at the given len, filled with the given value, mainly used internally

  • len - length of array
  • val - fill value (optional)

Example:

AsciiTable.arrayFill(4, 0) // [0, 0, 0, 0]

Instance Methods

instance.setBorder([edge], [fill], [top], [bottom])

Set the border characters for rendering, if no arguments are passed it will be reset to defaults. If a single edge arg is passed, it will be used for all borders.

  • edge - horizontal edges (optional, default |)
  • fill - vertical edges (optional, default -)
  • top - top corners (optional, default .)
  • bottom - bottom corners (optional, default ')

Example:

var table = new AsciiTable('Stars')
table
  .setBorder('*')
  .setHeading('oh', 'look')
  .addRow('so much', 'star power')

console.log(table.toString())
************************
*        Stars         *
************************
*   oh    *    look    *
************************
* so much * star power *
************************

instance.removeBorder()

Example:

table.removeBorder()

console.log('' + table)
  #     Fruit           Thing
 --- ----------- --------------------
  a       apple   Some longer string
  b      banana           hi
  c      carrot          meow
  e   elephants

instance.setAlign(idx, direction)

  • idx - column index to align
  • direction - alignment direction, (AsciiTable.LEFT, AsciiTable.CENTER, AsciiTable.RIGHT)

Example:

table
  .setAlign(2, AsciiTable.RIGHT)
  .setAlign(1, AsciiTable.CENTER)

console.log(table.toString())
.-------------------------------------.
| a  |   apple   | Some longer string |
| b  |   banana  |                 hi |
| c  |   carrot  |               meow |
| e  | elephants |                    |
'-------------------------------------'

instance.setAlignLeft(idx)

Alias to instance.setAlign(idx, AsciiTable.LEFT)

instance.setAlignCenter(idx)

Alias to instance.setAlign(idx, AsciiTable.CENTER)

instance.setAlignRight(idx)

Alias to instance.setAlign(idx, AsciiTable.RIGHT)

instance.setTitle(title)

  • title - table title

Example:

var table = new AsciiTable('Old Title')

table.setTitle('New Title')

instance.getTitle()

Get the current title of the table

Example:

table.getTitle() // 'New Title'

instance.setTitleAlign(direction)

  • direction - table alignment direction

Example:

instance.setTitleAlignLeft()

Alias to instance.setTitleAlign(AsciiTable.LEFT)

instance.setTitleAlignCenter()

Alias to instance.setTitleAlign(AsciiTable.CENTER)

instance.setTitleAlignRight()

Alias to instance.setTitleAlign(AsciiTable.RIGHT)

instance.sort(iterator)

  • iterator - sorting method to run against the rows

Example:

table.sort(function(a, b) {
  return a[2] - b[2]
})
console.log(table.toString())
.----------------.
| 2 | John |  34 |
| 1 | Bob  |  52 |
| 3 | Jim  |  83 |
'----------------'

instance.sortColumn(index, iterator)

Sorting shortcut for targeting a specific column

  • index - column idx to sort
  • iterator - sorting method to run against column values

Example:

// This is quivalent to the `sort` example above
table.sortColumn(2, function(a, b) {
  return a - b
})

instance.setHeading(heading, [...])

Set the column headings for the table, takes arguments the same way as addRow

  • heading - heading array or arguments

Example:

table.setHeading('ID', 'Key', 'Value')

// or:

table.setHeading(['ID', 'Key', 'Value'])

instance.setHeadingAlign(direction)

  • direction -

Example:

instance.setHeadingAlignLeft()

Alias to instance.setHeadingAlignLeft(AsciiTable.LEFT)

instance.setHeadingAlignCenter()

Alias to instance.setHeadingAlignLeft(AsciiTable.CENTER)

instance.setHeadingAlignRight()

Alias to instance.setHeadingAlignLeft(AsciiTable.RIGHT)

instance.addRow(row, [...])

Rows can be added using a single array argument, or the arguments if multiple args are used when calling the method.

  • row - array or arguments of column values

Example:

var table = new AsciiTable()

table
  .addRow(1, 'Bob', 52)
  .addRow([2, 'John', 34])

console.log(table.render())
.---------------.
| 1 | Bob  | 52 |
| 2 | John | 34 |
'---------------'

instance.addRowMatrix(rows)

Bulk addRow operation

  • rows - multidimentional array of rows

Example:

table.addRowMatrix([
  [2, 'John', 34]
, [3, 'Jim', 83]
])

instance.setJustify(enabled)

Justify all columns to be the same width

  • enabled - boolean for turning justify on or off, undefined considered true

Example:

table
  .addRow('1', 'two', 'three')
  .setJustify()

console.log(table.toString())
.-----------------------.
| 1     | two   | three |
'-----------------------'

instance.toString()

Render the instance as a string for output

Alias: [valueOf, render]

instance.toJSON()

Return the JSON representation of the table, this also allows us to call JSON.stringify on the instance.

Example:

var table = new AsciiTable('Title')

table
  .setHeading('id', 'name')
  .addRow(1, 'Bob')
  .addRow(2, 'Steve')

console.log(table.toJSON())
console.log(JSON.stringify(table))
{
  title: 'Title'
, heading: [ 'id', 'name' ]
, rows: [ 
    [ 1, 'Bob' ]
  , [ 2, 'Steve' ] 
  ] 
}
{"title":"Title","heading":["id","name"],"rows":[[1,"Bob"],[2,"Steve"]]}

instance.fromJSON(obj)

Populate the table from json object, should match the toJSON output above.

Alias: [parse]

Example:

var table = new AsciiTable().fromJSON({
  title: 'Title'
, heading: [ 'id', 'name' ]
, rows: [ 
    [ 1, 'Bob' ]
  , [ 2, 'Steve' ] 
  ] 
})

instance.clear()

Clear / reset all table data

Alias: [reset]

instance.clearRows()

Reset all row data, maintains title and headings.

Install

With npm

npm install ascii-table

Contributors

Matthew Oliveira Peter Daum

License

(The MIT License)

Copyright (c) 2013 Beau Sorensen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.