Code Monkey home page Code Monkey logo

table-cli's Introduction

GENERATE TABLE

A function to generate a unicode table for presenting data.

Details

The function will accept a JavaScript object with the following properties:

Name Type Required Comment
data Array true an array of arrays detailing the rows and columns of the table.
markdown Boolean false will default to unicode table if not set (or set to false).
headers Boolean false will convert top row to uppercase if set to true.
basic Boolean false will convert to a basic table format if set to true.
var data = [
    [ 'Name',    'Age' ],
    [ 'Richard', '30' ],
    [ 'John',    '31' ],
];

genTable({
    data:     data,
    markdown: false,
    headers:  false
});

The function will calculate max the width of each column and then the height of each row (line breaks within a cell). This will then allow the size of the table to be calculated. Any text that is not as wide as the max value will be increased with spaces.

Special characters should not be put into the cells (e.g. arrows, emojis) as they are not a standard width and can not be accounted for.

If the cells contain any tab \t characters they will be replace by 4 space characters instead. This will keep the formatting of the text but make it easier to calculate the width.

If the markdown property is set to true then the table will be outputed in the standard Markdown format as shown below. This should not be used for tables with cells that contain line breaks.

| NAME    | AGE |
| ------- | --- |
| Richard | 30  |
| John    | 31  |

If the headers property is set to true the top row of the table will be set to uppercase. It is not expected that headers would contain line breaks.

See below a sample of a basic table (with a markdown like format) that will be generated if the basic property is set to true.

*----------*------*
| NAME     | AGE  |
|----------|------|
| Richard  | 30   |
|----------|------|
| John     | 31   |
*----------*------*

Examples

Table of data:

sample_table_1

Captured data with header:

sample_table_2

Note: if including code in the table please ensure an special characters are negated e.g. \n as \\n

table-cli's People

Contributors

7wmr avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.