npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

markdown-table-ts

v1.0.3

Published

A zero-dependency library for generating Markdown tables written in TypeScript.

Downloads

83,042

Readme

markdown-table-ts

pipeline coverage license version downloads

markdown-table-ts is a zero-dependency library for generating Markdown tables. Since it's written in TypeScript, it comes with type declarations out of the box.

Installation

Grab markdown-table-ts from npm with your favorite package manager.

# npm
npm install markdown-table-ts

# yarn
yarn add markdown-table-ts

Quick start

// Node.js
// const { Align, getMarkdownTable } = require('markdown-table-ts');

// Node.js with TypeScript
import { Align, getMarkdownTable } from 'markdown-table-ts';

const table = getMarkdownTable({
  table: {
    head: ['ID', 'Name', 'Age'],
    body: [
      ['1', 'John',  '26'],
      ['2', 'Bob',   '25'],
      ['3', 'Alice', '23'],
    ],
  },
  alignment: [Align.Left, Align.Center, Align.Right],
});

console.log(table);

// Output:
// | ID  | Name  | Age |
// | :-- | :---: | --: |
// | 1   | John  |  26 |
// | 2   |  Bob  |  25 |
// | 3   | Alice |  23 |

API

getMarkdownTable(params: GetTableInput): string

  • generates the corresponding Markdown table based on params
  • does not append newline to the end of the resulting table
  • throws a MarkdownTableError when something goes wrong.

The underlying structure of GetTableInput looks like this.

enum Align {
  Left   = 'left',
  Right  = 'right',
  Center = 'center',
  None   = 'none',
}

type Column = string;
type Row    = Column[];

interface Table {
  head: Row,
  body: Row[],
}

interface GetTableInput {
  table:         Table,

  alignColumns?: boolean,
  alignment?:    Align[],
}

params.table.head

  • table headers
  • must be an array of strings
  • cannot be empty
Examples
// OK
['header1', 'header2', 'header3']

// Error
undefined
[]

params.table.body

  • table body
  • must be an array whose elements are arrays of strings
  • can be empty
Examples
// OK
['column1', 'column2', 'column3']
[]

// Error
undefined

params.alignColumns

  • whether to vertically align columns (|)
  • must be boolean or undefined (in which case it will default to true)
Examples
// OK
true
false
undefined

// Error
'yes'
1
alignColumns on/off
import { Align, Table, getMarkdownTable } from 'markdown-table-ts';

const myTable: Table = {
  head: ['first header', 'second header', 'very very very long header'],
  body: [
    ['1',                      'John',  '26'],
    ['very very very long id', 'Bob',   '25'],
    ['3',                      'Alice', '23'],
  ],
};

const myAlignment: Align[] = [Align.Center, Align.None, Align.Right];

console.log(getMarkdownTable({
  table: myTable,
  alignment: myAlignment,

  alignColumns: true,
}));

console.log(getMarkdownTable({
  table: myTable,
  alignment: myAlignment,

  alignColumns: false,
}));

// alignColumns on
// |      first header      | second header | very very very long header |
// | :--------------------: | ------------- | -------------------------: |
// |           1            | John          |                         26 |
// | very very very long id | Bob           |                         25 |
// |           3            | Alice         |                         23 |

// alignColumns off
// | first header | second header | very very very long header |
// | :-: | --- | --: |
// | 1 | John | 26 |
// | very very very long id | Bob | 25 |
// | 3 | Alice | 23 |

params.alignment

  • defines how to align each of the columns
  • must be an array of Align which is one of
    • Align.Left (:----)
    • Align.Right (----:)
    • Align.Center (:---:)
    • Align.None (-----)
  • defaults to Align.None
Examples
// OK
[Align.Left, Align.Right, Align.Center, Align.None]

// Not recommended but this will also pass if you turn off type checking
['left', 'right', 'center', 'none']

// Error
[Align.left, 1, 'hello']

Edge cases

Some rows have more columns than others

When some of the rows (table head included) have more columns than others, the resulting table will have as many columns as the row with the most columns. Undefined columns will be empty.

import { getMarkdownTable } from 'markdown-table-ts';

const table = getMarkdownTable({
  table: {
    head: ['ID', 'Name', 'Age'],
    body: [
      ['1', 'John',  '26', 'more data', 'even more data'],
      ['2', 'Bob',   '25'],
      ['3', 'Alice', '23', 'extra column'],
    ],
  },
});

console.log(table);

// Output:
// | ID  | Name  | Age |              |                |
// | --- | ----- | --- | ------------ | -------------- |
// | 1   | John  | 26  | more data    | even more data |
// | 2   | Bob   | 25  |              |                |
// | 3   | Alice | 23  | extra column |                |

Length of alignment array is less than amount of columns in the table

When there are less elements in the params.alignment array than there are columns in the resulting table, getMarkdownTable will still succeed and columns with undefined alignment will default to Align.None (-----), e.g. when there are 4 columns in the table and the length of params.alignment is 2, the first 2 columns will use alignment from params.alignment and the rest of the columns will default to Align.None (-----).

import { Align, getMarkdownTable } from 'markdown-table-ts';

const table = getMarkdownTable({
  table: {
    head: ['ID', 'Name', 'Age', 'Country'],
    body: [
      ['1', 'John',  '26', 'Czechia'],
      ['2', 'Bob',   '25', 'United States'],
      ['3', 'Alice', '23', 'Denmark'],
    ],
  },
  alignment: [Align.Center, Align.Left],
});

console.log(table);

// Output:
// | ID  | Name  | Age | Country       |
// | :-: | :---- | --- | ------------- |
// |  1  | John  | 26  | Czechia       |
// |  2  | Bob   | 25  | United States |
// |  3  | Alice | 23  | Denmark       |