Render data in a width adjustable ascii table.
This module provides functionality to render tables with text borders / outlines so it can be pasted into the medium of choice.
The configuration is very limited by design, all that's configurable in the current version is the maximun width of the columns.
The API exposes only two methods to render a table:
table(rows, [maxColWidth]) where
rows is expected to be
an array with an index for every row, and each row is also expected to be an array
since it will be serialized before printed.
The second method to generate a table is:
serializedRows is expected to be in the same format as the previously described method,
but all data must already be serialized. This method should be used when the data stays the same
but are generated with multiple maxColWidths.
To serialize the data, the method
serializeData(rows) is exposed. For the moment, all it does is to
JSON.stringify on the data.
To get the width of the widest column (can be used to set the max value on a slider),
is exposed. The rows should not already be serialized when calling this method.
All rows should have the same number of columns, and the first row is expected to be the header column with titles for each column.
'first column' 'second column' // title row'my data row 1 col 1' 'my data row 1 col 2' // first data row'my data row 2 col 1' 'my data row 2 col 2' // second data row
With default max width, the above would produce:
╒═════════════════════╤═════════════════════╕│"first column" │"second column" │╞═════════════════════╪═════════════════════╡│"my data row 1 col 1"│"my data row 1 col 2"│├─────────────────────┼─────────────────────┤│"my data row 2 col 1"│"my data row 2 col 2"│└─────────────────────┴─────────────────────┘
npm in your working directory:
npm install ascii-data-table --save
bower in your working directory:
bower install ascii-data-table --save
Two packages are produced, one for Node.js environment and one for web browsers.
Usage in Node.js varies depending if the will be used within a ES2015 application or not.
// If install with npm// or if installed by cloning git repo, use the correct path//import AsciiTable from 'lib/ascii-data-table'// The data to renderconst items = 'x' 'y' 'a' 'b' 'c' 'd'// Not required, default is 30const maxColumnWidth = 15// Render and save in 'res'const res = AsciiTable
In ES 5.5
// If install with npmvar AsciiTable = default// or if installed by cloning git repo, use the correct path//var AsciiTable = require('lib/ascii-data-table').defaultvar items = 'x' 'y' 'a' 'b' 'c' 'd'var res = AsciiTable
A bundle for web browsers is created and can be found in
A functional / stateless React Component is created and lies in
assumes there's a global varaible
A bundle for Angular 1.X is created and can be found in
assumes there's a global variable named
You can try online here: Online demo
examples folder there are examples for node and web browser environments.
One cool thing in the browser demo is that you can hook up a range slider to the maximun width of the columns, giving this effect:
npm test to execute test in both Node.js and browser environments.
npm table test:watch to have tests table on file changes.