diff options
| author | Bobby <[email protected]> | 2022-01-26 13:57:40 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-01-26 13:57:40 -0500 |
| commit | 901901f0cc4795305d368f588a2d18fe2a8e28b2 (patch) | |
| tree | dbaf30463ef987e9d6727b3afe783e3d53021b50 /src/lib | |
| parent | 235a6523cc3239193bb908ae91da7426929d79ab (diff) | |
| download | izuku.js-901901f0cc4795305d368f588a2d18fe2a8e28b2.tar.xz izuku.js-901901f0cc4795305d368f588a2d18fe2a8e28b2.zip | |
feat: inbuilt table functionality
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/display.ts | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/lib/display.ts b/src/lib/display.ts index 4349aa5..2ec19d9 100644 --- a/src/lib/display.ts +++ b/src/lib/display.ts @@ -1,5 +1,6 @@ import { Frame } from '../index'; import { table } from 'table'; +import { Table } from '../helpers/tableBuilder'; /** * getTable returns the data compatible with table.table @@ -35,6 +36,40 @@ function getTable( } /** + * displayTable prints the table to the console + * @param rowdata the rowdata to be sent to the frame + */ +export function displayTable(rowdata: any[][]): void { + // Convert row data into object with keys and values, keys are the column names stored in rowdata[0] + const headerObject: any = []; + rowdata[0].forEach((column) => { + headerObject.push({ + name: column, + alignment: 'left', + paddingLeft: 2, + bold: true, + paddingRight: 2 + }); + }); + const table = new Table({ + columns: headerObject + }); + + const rowdataObject = rowdata.map((row) => { + const rowObject: any = {}; + row.forEach((value, index) => { + rowObject[rowdata[0][index]] = value; + }); + return rowObject; + }); + + // remove the first row from rowdataObject + const rowdataObjectWithoutHeader = rowdataObject.slice(1); + table.addRows(rowdataObjectWithoutHeader); + table.printTable(); +} + +/** * show prints the frame in console.table format * @returns the current frame * @throws Error if the frame is empty @@ -63,7 +98,7 @@ export function show(this: Frame): void { numberOfRows - 1 ]; const combinedRow = [...firstThreeRows, [...middleRow], ...lastThreeRows]; - console.log(table(getTable(combinedRow, this.columns, indexRow))); + displayTable(getTable(combinedRow, this.columns, indexRow)); } } } |
