Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tablemark

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tablemark

Generate markdown tables from a list of objects or JSON data.

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
41K
decreased by-0.95%
Maintainers
2
Weekly downloads
 
Created
Source

tablemark · Version License TypeScript GitHub Actions

Generate markdown tables from JSON data.

Renders arrays of objects as markdown tables, complete with configuration for renaming columns and left, center, or right-aligning them.

installation

yarn add tablemark

# or

npm install tablemark

usage

import tablemark from "tablemark"
tablemark([
  { name: "Bob", age: 21, isCool: false },
  { name: "Sarah", age: 22, isCool: true },
  { name: "Lee", age: 23, isCool: true }
])

// | Name  | Age   | Is cool |
// | :---- | :---- | :------ |
// | Bob   | 21    | false   |
// | Sarah | 22    | true    |
// | Lee   | 23    | true    |

... displays as:

NameAgeIs cool
Bob21false
Sarah22true
Lee23true

api

tablemark (input: InputData, options?: TablemarkOptions): string

Arguments

  • InputData input: the data to table-ify

    • an array of objects or iterables
  • TablemarkOptions options:

    keytypedefaultdescription
    columnsArray<string | ColumnDescriptor>-Array of column descriptors.
    caseHeadersbooleantrueSentence case headers derived from keys.
    toCellText(input: unknown) => string-Provide a custom "toString" function.
    padHeaderSeparatorbooleantrueWhether to pad gutters of the header separator (alignment) row.
    wrapWidthnumberInfinityWrap cell text at this length.
    wrapWithGuttersbooleanfalseAdd sides (| <content> |) to wrapped rows.
    lineEndingstring"\n"String used at end-of-line.

The columns array can either contain objects, in which case their name and align properties will be used to alter the display of the column in the table, or any other type which will be coerced to a string if necessary and used as a replacement for the column name.

Returns

string: the resulting markdown formatted table

Throws

TypeError: when input is not iterable (e.g., an array)
TypeError: when an unknown column alignment option is provided

options.columns

tablemark(
  [
    { name: "Bob", age: 21, isCool: false },
    { name: "Sarah", age: 22, isCool: true },
    { name: "Lee", age: 23, isCool: true }
  ],
  {
    columns: [
      "first name",
      { name: "how old", align: "center" },
      "are they cool"
    ]
  }
)

// | first name | how old | are they cool |
// | :--------- | :-----: | :------------ |
// | Bob        |   21    | false         |
// | Sarah      |   22    | true          |
// | Lee        |   23    | true          |

... displays as:

first namehow oldare they cool
Bob21false
Sarah22true
Lee23true

options.padHeaderSeparator

tablemark(
  [
    { name: "Bob", age: 21, isCool: false },
    { name: "Sarah", age: 22, isCool: true },
    { name: "Lee", age: 23, isCool: true }
  ],
  {
    columns: [{ align: "left" }, { align: "center" }, { align: "right" }]
  }
)

// | first name | how old | are they cool |
// |:-----------|:-------:|--------------:|
// | Bob        |   21    | false         |
// | Sarah      |   22    | true          |
// | Lee        |   23    | true          |

... displays as:

first namehow oldare they cool
Bob21false
Sarah22true
Lee23true

options.toCellText

tablemark(
  [
    { name: "Bob", pet_owner: true, studying: false },
    { name: "Sarah", pet_owner: false, studying: true },
    { name: "Sarah", pet_owner: true, studying: true }
  ],
  {
    toCellText,
    columns: [{ align: "left" }, { align: "center" }, { align: "center" }]
  }
)

function toCellText(v) {
  if (v === true) return "✔"
  if (!v) return ""
  return v
}

// | Name  | Pet owner | Studying |
// | :---- | :-------: | :------: |
// | Bob   |     ✔︎     |          |
// | Sarah |           |    ✔     |
// | Lee   |     ✔     |    ✔     |

options.wrapWidth

Set options.wrapWidth to wrap any content at that length onto a new adjacent line:

tablemark(
  [
    { star: false, name: "Benjamin" },
    { star: true, name: "Jet Li" }
  ],
  { wrapWidth: 5 }
)

// | Star  | Name  |
// | :---- | :---- |
// | false | Benja |
//           min
// | true  | Jet   |
//           Li

To output valid GitHub Flavored Markdown a cell must not contain newlines. Consider replacing those with <br /> (e.g., using options.toCellText).

options.wrapWithGutters

Enable wrapWithGutters to add pipes on all lines:

tablemark(
  [
    { star: false, name: "Benjamin" },
    { star: true, name: "Jet Li" }
  ],
  { wrapWidth: 5, wrapWithGutters: true }
)

// | Star  | Name  |
// | :---- | :---- |
// | false | Benja |
// |       | min   |
// | true  | Jet   |
// |       | Li    |

see also

contributing

Search the issues if you come across any trouble, open a new one if it hasn't been posted, or, if you're able, open a pull request. Contributions of any kind are welcome in this project.

The following people have already contributed their time and effort:

Thank you!

license

MIT © Bo Lingen / haltcase

Keywords

FAQs

Package last updated on 02 Apr 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc