Socket
Socket
Sign inDemoInstall

table-layout

Package Overview
Dependencies
4
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    table-layout

Stylable text tables, handling ansi colour. Useful for console output.


Version published
Maintainers
1
Install size
93.7 kB
Created

Package description

What is table-layout?

The table-layout package is a utility for Node.js that allows for the easy formatting of text into a table structure. It provides a way to organize data in rows and columns, with options for customizing alignment, width, and padding. This package is useful for CLI tools and scripts that need to present data in a structured, readable format.

What are table-layout's main functionalities?

Basic table layout

This feature allows you to create a basic table layout by specifying an array of objects, where each object represents a row in the table. The keys of the object represent column names.

const Table = require('table-layout');
const data = [{ colA: 'row 1 column A', colB: 'row 1 column B' }, { colA: 'row 2 column A', colB: 'row 2 column B' }];
const table = new Table(data);
console.log(table.toString());

Custom column width and wrapping

This feature demonstrates how to customize the width of columns and enable text wrapping. It is useful for controlling the appearance of the table and ensuring that it fits within a specific layout.

const Table = require('table-layout');
const data = [{ name: 'Some really long name that exceeds the default column width', age: '30' }];
const options = { columns: [{ name: 'name', width: 30, nowrap: true }, { name: 'age', width: 10 }] };
const table = new Table(data, options);
console.log(table.toString());

Other packages similar to table-layout

Readme

Source

view on npm npm module downloads Gihub repo dependents Gihub package dependents Build Status Coverage Status js-standard-style

table-layout

Generates plain-text tables from JSON recordset input (array of objects). Useful for presenting text in column layout or data in table layout in text-based user interfaces. Also available as a command-line tool.

Synopsis

Where input looks like this:

[
  {
    "number": 15134,
    "title": "Coveralls has no source available ",
    "login": "ndelangen",
    "comments": 0
  },
  {
    "number": 15133,
    "title": "Fixing --preserve-symlinks. Enhancing node to exploit.",
    "login": "phestermcs",
    "comments": 0
  },
  {
    "number": 15131,
    "title": "Question - Confused about NPM's local installation philosophy",
    "login": "the1mills",
    "comments": 0
  },
  {
    "number": 15130,
    "title": "Question - global npm cache directory if user is root?",
    "login": "ORESoftware",
    "comments": 0
  }
]

This code...

const Table = require('table-layout')
const issues = require('./issues.json')
const table = new Table(issues, { maxWidth: 60 })
console.log(table.toString())

...produces this output:

 15134  Coveralls has no source available   ndelangen     0
 15133  Fixing --preserve-symlinks.         phestermcs    0
        Enhancing node to exploit.
 15131  Question - Confused about NPM's     the1mills     0
        local installation philosophy
 15130  Question - global npm cache         ORESoftware   0
        directory if user is root?
 15127  how to installa gulp fontfacegen    aramgreat     0
        on Windows 10
 15097  Cannot install package from         mastertinner  3
        tarball out of package.json entry
        generated by npm
 15067  npm "SELF_SIGNED_CERT_IN_CHAIN"     LegendsLyfe   3
        error when installing discord.js
        with .log

API Reference

Table ⏏

Recordset data in (array of objects), text table out.

Kind: Exported class

new Table(data, [options])

Params

  • data Array.<object> - input data
  • [options] object - optional settings
    • [.maxWidth] number - maximum width of layout
    • [.noWrap] boolean - disable wrapping on all columns
    • [.noTrim] boolean - disable line-trimming
    • [.break] boolean - enable word-breaking on all columns
    • [.columns] columnOption - array of column-specific options
    • [.ignoreEmptyColumns] boolean - if set, empty columns or columns containing only whitespace are not rendered.
    • [.padding] object - Padding values to set on each column. Per-column overrides can be set in the options.columns array.
      • [.left] string - Defaults to a single space.
      • [.right] string - Defaults to a single space.

Example

> Table = require('table-layout')
> jsonData = [{
  col1: 'Some text you wish to read in table layout',
  col2: 'And some more text in column two. '
}]
> table = new Table(jsonData, { maxWidth: 30 })
> console.log(table.toString())
 Some text you  And some more
 wish to read   text in
 in table      column two.
 layout

table.renderLines() ⇒ Array.<string>

Identical to .toString() with the exception that the result will be an array of lines, rather than a single, multi-line string.

Kind: instance method of Table

table.toString() ⇒ string

Returns the input data as a text table.

Kind: instance method of Table

Table~columnOption

Kind: inner typedef of Table
Properties

NameTypeDescription
namestringcolumn name, must match a property name in the input
[width]numberA specific column width. Supply either this or a min and/or max width.
[minWidth]numbercolumn min width
[maxWidth]numbercolumn max width
[nowrap]booleandisable wrapping for this column
[break]booleanenable word-breaking for this columns
[padding]objectpadding options
[padding.left]stringa string to pad the left of each cell (default: ' ')
[padding.right]stringa string to pad the right of each cell (default: ' ')

© 2015-21 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.

Keywords

FAQs

Last updated on 07 Mar 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc