Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

fix(table): Allow user to override tableoptions #40

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/styled/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ export interface TableOptions {
*/
export default function table(data: any[], inputOptions: Partial<TableOptions> = {}) {
const options: TableOptions = {
colSep: ' ',
after: () => {},
headerAnsi: _.identity,
printLine: (s: any) => console.log(s),
printRow(cells: any[]) {
this.printLine((cells.join(this.colSep) as any).trimRight())
},
printHeader(cells: any[]) {
this.printRow(cells.map(_.ary(this.headerAnsi, 1)))
this.printRow(cells.map(hdr => hdr.replace(/./g, '─')))
},
...inputOptions,
columns: (inputOptions.columns || []).map(c => ({
format: (value: any) => (value != null ? value.toString() : ''),
Expand All @@ -68,17 +79,6 @@ export default function table(data: any[], inputOptions: Partial<TableOptions> =
},
...c,
})),
colSep: ' ',
after: () => {},
headerAnsi: _.identity,
printLine: (s: any) => console.log(s),
printRow(cells: any[]) {
this.printLine((cells.join(this.colSep) as any).trimRight())
},
printHeader(cells: any[]) {
this.printRow(cells.map(_.ary(this.headerAnsi, 1)))
this.printRow(cells.map(hdr => hdr.replace(/./g, '─')))
},
}

function calcWidth(cell: any) {
Expand Down
21 changes: 21 additions & 0 deletions test/styled/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ describe('styled/table', () => {
1 1
2 2
3 3
`)
})

fancy
.stdout()
.end('hides the header', output => {
cli.table([
{foo: 1, bar: 1},
{foo: 2, bar: 2},
{foo: 3, bar: 3},
], {
printHeader: undefined,
columns: [
{key: 'bar'},
{key: 'foo'},
]
})

expect(output.stdout).to.equal(`1 1
2 2
3 3
`)
})
})