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

Allow multiple filter flags in Table #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 16 additions & 14 deletions src/styled/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ class Table<T extends object> {
})

// filter rows
if (this.options.filter) {
/* eslint-disable-next-line prefer-const */
let [header, regex] = this.options.filter!.split('=')
const isNot = header[0] === '-'
if (isNot) header = header.substr(1)
const col = this.findColumnFromHeader(header)
if (!col || !regex) throw new Error('Filter flag has an invalid value')
rows = rows.filter((d: any) => {
const re = new RegExp(regex)
const val = d[col!.key]
const match = val.match(re)
return isNot ? !match : match
if (this.options.filter && !_.isEmpty(this.options.filter)) {
this.options.filter.forEach(filter => {
/* eslint-disable-next-line prefer-const */
let [header, regex] = filter.split('=')
const isNot = header[0] === '-'
if (isNot) header = header.substr(1)
const col = this.findColumnFromHeader(header)
if (!col || !regex) throw new Error(`Filter flag "${header}" has an invalid value`)
rows = rows.filter((d: any) => {
const re = new RegExp(regex)
const val = d[col!.key]
const match = val.match(re)
return isNot ? !match : match
})
})
}

Expand Down Expand Up @@ -287,7 +289,7 @@ export namespace table {
export const Flags = {
columns: F.string({exclusive: ['extended'], description: 'only show provided columns (comma-separated)'}),
sort: F.string({description: 'property to sort by (prepend \'-\' for descending)'}),
filter: F.string({description: 'filter property by partial string matching, ex: name=foo'}),
filter: F.string({description: 'filter property by partial string matching, ex: name=foo', multiple: true}),
csv: F.boolean({exclusive: ['no-truncate'], description: 'output is csv format [alias: --output=csv]'}),
output: F.string({
exclusive: ['no-truncate', 'csv'],
Expand Down Expand Up @@ -335,7 +337,7 @@ export namespace table {
export interface Options {
[key: string]: any;
sort?: string;
filter?: string;
filter?: string[];
columns?: string;
extended?: boolean;
'no-truncate'?: boolean;
Expand Down
6 changes: 3 additions & 3 deletions test/styled/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('styled/table', () => {
fancy
.stdout()
.end('filters by property & value (partial string match)', output => {
cli.table(apps, columns, {filter: 'id=123'})
cli.table(apps, columns, {filter: ['id=123']})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
123 supertable-test-1${ws}\n`)
})
Expand All @@ -242,7 +242,7 @@ describe('styled/table', () => {
.stdout()
.end('does not truncate', output => {
const three = {...apps[0], id: '0'.repeat(80), name: 'supertable-test-3'}
cli.table(apps.concat(three), columns, {filter: 'id=0', 'no-truncate': true})
cli.table(apps.concat(three), columns, {filter: ['id=0'], 'no-truncate': true})
expect(output.stdout).to.equal(`ID${ws.padEnd(78)} Name${ws.padEnd(14)}
${three.id} supertable-test-3${ws}\n`)
})
Expand All @@ -268,7 +268,7 @@ ${three.id} supertable-test-3${ws}\n`)
fancy
.stdout()
.end('ignores header case', output => {
cli.table(apps, columns, {columns: 'iD,Name', filter: 'nAMe=supertable-test', sort: '-ID'})
cli.table(apps, columns, {columns: 'iD,Name', filter: ['nAMe=supertable-test'], sort: '-ID'})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
321 supertable-test-2${ws}
123 supertable-test-1${ws}\n`)
Expand Down