Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ascii-table
Advanced tools
The ascii-table npm package is a simple and lightweight library for creating and formatting ASCII tables in Node.js. It allows you to easily generate tables with headers, rows, and columns, and provides various customization options for table appearance.
Creating a Basic Table
This feature allows you to create a basic ASCII table with a title, headers, and rows. The code sample demonstrates how to set up a table with a title 'Sample Table', add headers 'Name', 'Age', and 'City', and add two rows of data.
const AsciiTable = require('ascii-table');
let table = new AsciiTable('Sample Table');
table.setHeading('Name', 'Age', 'City');
table.addRow('Alice', 25, 'New York');
table.addRow('Bob', 30, 'Los Angeles');
console.log(table.toString());
Customizing Table Appearance
This feature allows you to customize the appearance of the table, such as aligning the text in different columns. The code sample shows how to align the first column to the left, the second column to the center, and the third column to the right.
const AsciiTable = require('ascii-table');
let table = new AsciiTable('Styled Table');
table.setHeading('Name', 'Age', 'City');
table.addRow('Alice', 25, 'New York');
table.addRow('Bob', 30, 'Los Angeles');
table.setAlign(0, AsciiTable.LEFT);
table.setAlign(1, AsciiTable.CENTER);
table.setAlign(2, AsciiTable.RIGHT);
console.log(table.toString());
Adding Rows Dynamically
This feature allows you to add rows to the table dynamically, which is useful when dealing with data from external sources. The code sample demonstrates how to add multiple rows from an array of data.
const AsciiTable = require('ascii-table');
let table = new AsciiTable('Dynamic Rows');
table.setHeading('Name', 'Age', 'City');
const data = [
['Alice', 25, 'New York'],
['Bob', 30, 'Los Angeles'],
['Charlie', 35, 'Chicago']
];
data.forEach(row => table.addRow(...row));
console.log(table.toString());
The cli-table package is another popular library for creating ASCII tables in Node.js. It offers more advanced features such as nested tables, custom styles, and support for different table layouts. Compared to ascii-table, cli-table provides more flexibility and customization options but may have a steeper learning curve.
The table package is a highly configurable library for generating text tables in Node.js. It supports features like text wrapping, column alignment, and custom border styles. Compared to ascii-table, the table package offers more comprehensive configuration options and is suitable for more complex table formatting needs.
The easy-table package is a lightweight library for creating ASCII tables with a focus on simplicity and ease of use. It provides basic table creation and formatting features similar to ascii-table but with a simpler API. Compared to ascii-table, easy-table is more straightforward and easier to use for basic table creation tasks.
Easy table output for node debugging, but you could probably do more with it, since its just a string.
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
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 | |
'------------------------------------'
See: AsciiTable.factory
for details on instantiation
Table instance creator
title
- table title (optional, default null
)options
- table options (optional)
prefix
- string prefix to add to each line on renderNote: 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' ]
]
})
Shortcut to one of the three following methods
direction
- alignment direction (AsciiTable.LEFT
, AsciiTable.CENTER
, AsciiTable.RIGHT
)val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default ' '
)Example:
table.align(AsciiTable.LEFT, 'hey', 7) // 'hey '
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default ' '
)Example:
table.alignLeft('hey', 7, '-') // 'hey----'
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default ' '
)Example:
table.alignCenter('hey', 7) // ' hey '
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default ' '
)Example:
table.alignRight('hey', 7) // ' hey'
Attempt to do intelligent alignment of provided val
, String
input will
be left aligned, Number
types will be right aligned.
val
- string to alignlen
- total length of created stringpad
- padding / fill char (optional, default ' '
)Example:
table.align(AsciiTable.LEFT, 'hey', 7) // 'hey '
Create a new array at the given len, filled with the given value, mainly used internally
len
- length of arrayval
- fill value (optional)Example:
AsciiTable.arrayFill(4, 0) // [0, 0, 0, 0]
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 *
************************
Example:
table.removeBorder()
console.log('' + table)
# Fruit Thing
--- ----------- --------------------
a apple Some longer string
b banana hi
c carrot meow
e elephants
idx
- column index to aligndirection
- 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 | |
'-------------------------------------'
Alias to instance.setAlign(idx, AsciiTable.LEFT)
Alias to instance.setAlign(idx, AsciiTable.CENTER)
Alias to instance.setAlign(idx, AsciiTable.RIGHT)
title
- table titleExample:
var table = new AsciiTable('Old Title')
table.setTitle('New Title')
Get the current title of the table
Example:
table.getTitle() // 'New Title'
direction
- table alignment directionExample:
Alias to instance.setTitleAlign(AsciiTable.LEFT)
Alias to instance.setTitleAlign(AsciiTable.CENTER)
Alias to instance.setTitleAlign(AsciiTable.RIGHT)
iterator
- sorting method to run against the rowsExample:
table.sort(function(a, b) {
return a[2] - b[2]
})
console.log(table.toString())
.----------------.
| 2 | John | 34 |
| 1 | Bob | 52 |
| 3 | Jim | 83 |
'----------------'
Sorting shortcut for targeting a specific column
index
- column idx to sortiterator
- sorting method to run against column valuesExample:
// This is quivalent to the `sort` example above
table.sortColumn(2, function(a, b) {
return a - b
})
Set the column headings for the table, takes arguments the same way as addRow
heading
- heading array or argumentsExample:
table.setHeading('ID', 'Key', 'Value')
// or:
table.setHeading(['ID', 'Key', 'Value'])
direction
-Example:
Alias to instance.setHeadingAlignLeft(AsciiTable.LEFT)
Alias to instance.setHeadingAlignLeft(AsciiTable.CENTER)
Alias to instance.setHeadingAlignLeft(AsciiTable.RIGHT)
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 valuesExample:
var table = new AsciiTable()
table
.addRow(1, 'Bob', 52)
.addRow([2, 'John', 34])
console.log(table.render())
.---------------.
| 1 | Bob | 52 |
| 2 | John | 34 |
'---------------'
Bulk addRow
operation
rows
- multidimentional array of rowsExample:
table.addRowMatrix([
[2, 'John', 34]
, [3, 'Jim', 83]
])
Justify all columns to be the same width
enabled
- boolean for turning justify on or off, undefined
considered trueExample:
table
.addRow('1', 'two', 'three')
.setJustify()
console.log(table.toString())
.-----------------------.
| 1 | two | three |
'-----------------------'
Render the instance as a string for output
Alias: [valueOf
, render
]
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"]]}
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' ]
]
})
Clear / reset all table data
Alias: [reset
]
Reset all row data, maintains title and headings.
With npm
npm install ascii-table
(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.
FAQs
Easy tables for your console data
The npm package ascii-table receives a total of 133,601 weekly downloads. As such, ascii-table popularity was classified as popular.
We found that ascii-table demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.