ohmd
v1.0.2
Published
Convert arrays and objects to Markdown tables.
Downloads
2
Maintainers
Readme
omd
:feather: :feet: Convert arrays and objects to Markdown tables.
Install
npm install ohmd
API
omd(data, options)
data
: The data to be converted into a Markdown table. It can be:- A single array.
- An object with header and table properties.
- A 2D array where the first sub-array is the header and the remaining sub-arrays are the table rows.
options
: Configuration options for the table formatting. It's optional.align
- Specifies the alignment for each column. Valid values areleft
,center
, andright
. If not provided, the default alignment is used.
Examples
- Markdown for a single array:
const omd = require('ohmd');
const arrayData = ['Jon', 'Doe'];
console.log(omd(arrayData));
// | --- |
// | Jon |
// | Doe |
console.log(omd(arrayData, { align: ['left'] }));
// | :--- |
// | Jon |
// | Doe |
- Markdown for an object with different alignments:
const objectData = {
header: ['Day', 'Rank', 'Grade'],
table: [['Tuesday', 7, "C Minus"], ['Friday', 1, "A Plus"], ['Dryday', 4, "B Minus"]],
};
console.log(omd(objectData, { align: ['left', 'center', 'right'] }));
// | Day | Rank | Grade |
// | :--- | :---: | ---: |
// | Tuesday | 7 | C Minus |
// | Friday | 1 | A Plus |
// | Dryday | 4 | B Minus |
- Markdown for an object with no header:
const objectWithoutHeader = {
"table": [
["Alice", 30, "New York"],
["Bob", 25, "San Francisco"],
["Charlie", 35, "Los Angeles"]
]
}
// | Alice | 30 | New York |
// | Bob | 25 | San Francisco |
// | Charlie | 35 | Los Angeles |
- Markdown for an object with single alignment:
console.log(omd(objectData, { align: ['center'] }));
// | Day | Rank | Grade |
// | :---: | :---: | :---: |
// | Tuesday | 7 | C Minus |
// | Friday | 1 | A Plus |
// | Dryday | 4 | B Minus |
- Markdown for a 2D array with different alignments:
const twoDArrayData = [
['name', 'class', 'number'],
['Jon', 7, 99],
['Ron', 13, 233],
['Akon', 12, 13],
];
console.log(omd(twoDArrayData, { align: ['center', 'left', 'right'] }));
// | name | class | number |
// | :---: | :--- | ---: |
// | Jon | 7 | 99 |
// | Ron | 13 | 233 |
// | Akon | 12 | 13 |
- Markdown for a 2D array with single alignment:
console.log(omd(twoDArrayData, { align: ['center'] }));
// | name | class | number |
// | :---: | :---: | :---: |
// | Jon | 7 | 99 |
// | Ron | 13 | 233 |
// | Akon | 12 | 13 |
Contributing
If you find any bugs or have suggestions for improvements, please feel free to submit a pull request or open an issue.