Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
markdown-table-ts
Advanced tools
A zero-dependency library for generating Markdown tables written in TypeScript.
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.
Grab markdown-table-ts from npm with your favorite package manager.
# npm
npm install markdown-table-ts
# yarn
yarn add markdown-table-ts
// 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 |
getMarkdownTable(params: GetTableInput): string
params
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
// OK
['header1', 'header2', 'header3']
// Error
undefined
[]
params.table.body
// OK
['column1', 'column2', 'column3']
[]
// Error
undefined
params.alignColumns
|
)// OK
true
false
undefined
// Error
'yes'
1
alignColumns
on/offimport { 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
Align
which is one of
Align.Left
(:----
)Align.Right
(----:
)Align.Center
(:---:
)Align.None
(-----
)Align.None
// 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']
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 | |
alignment
array is less than amount of columns in the tableWhen 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 |
FAQs
A zero-dependency library for generating Markdown tables written in TypeScript.
The npm package markdown-table-ts receives a total of 11,192 weekly downloads. As such, markdown-table-ts popularity was classified as popular.
We found that markdown-table-ts demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.