-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from kidroca/kidroca/md-print-format
`Onxy.printMetrics` MD, CSV and JSON formats
- Loading branch information
Showing
5 changed files
with
173 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import AsciTable from 'ascii-table'; | ||
|
||
class MDTable extends AsciTable { | ||
/** | ||
* Create a CSV string from the table data | ||
* @returns {string} | ||
*/ | ||
toCSV() { | ||
return [this.getTitle(), this.getHeading(), ...this.getRows()].join('\n'); | ||
} | ||
|
||
/** | ||
* Create a JSON string from the table data | ||
* @returns {string} | ||
*/ | ||
toJSON() { | ||
return JSON.stringify(super.toJSON()); | ||
} | ||
|
||
/** | ||
* Create a MD string from the table data | ||
* @returns {string} | ||
*/ | ||
toString() { | ||
// Ignore modifying the first |---| for titled tables | ||
let idx = this.getTitle() ? -2 : -1; | ||
const ascii = super.toString() | ||
.replace(/-\|/g, () => { | ||
/* we replace "----|" with "---:|" to align the data to the right in MD */ | ||
idx++; | ||
|
||
if (idx < 0 || this.leftAlignedCols.includes(idx)) { | ||
return '-|'; | ||
} | ||
|
||
return ':|'; | ||
}); | ||
|
||
// strip the top and the bottom row (----) to make an MD table | ||
const md = ascii.split('\n').slice(1, -1).join('\n'); | ||
return md; | ||
} | ||
} | ||
|
||
/** | ||
* Table Factory helper | ||
* @param {Object} options | ||
* @param {string} [options.title] - optional title center above the table | ||
* @param {string[]} options.heading - table column names | ||
* @param {number[]} [options.leftAlignedCols=[]] - indexes of columns that should be left aligned | ||
* Pass the columns that are non numeric here - the rest will be aligned to the right | ||
* @param {Array} [options.rows] The table can be initialized with row. Rows can also be added by `addRow` | ||
* @returns {MDTable} | ||
*/ | ||
MDTable.factory = ({ | ||
title, heading, leftAlignedCols = [], rows = [] | ||
}) => { | ||
const table = new MDTable({title, heading, rows}); | ||
table.leftAlignedCols = leftAlignedCols; | ||
|
||
/* By default we want everything aligned to the right as most values are numbers | ||
* we just override the columns that are not right aligned */ | ||
heading.forEach((name, idx) => table.setAlign(idx, AsciTable.RIGHT)); | ||
leftAlignedCols.forEach(idx => table.setAlign(idx, AsciTable.LEFT)); | ||
|
||
return table; | ||
}; | ||
|
||
export default MDTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters