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

Commit

Permalink
Revert "feat: support multiple filter flags for table (#156)" (#231)
Browse files Browse the repository at this point in the history
This reverts commit 057c852.
  • Loading branch information
Milad Imen committed Aug 28, 2020
1 parent 057c852 commit c008a42
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
32 changes: 15 additions & 17 deletions src/styled/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,18 @@ class Table<T extends object> {
})

// filter rows
if (!_.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 has an invalid value')
rows = rows.filter((d: any) => {
const re = new RegExp(regex)
const val = d[col!.key]
const match = re.test(val)
return isNot ? !match : match
})
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
})
}

Expand Down Expand Up @@ -291,7 +289,7 @@ export namespace table {
export const Flags: {
columns: F.IOptionFlag<string | undefined>;
sort: F.IOptionFlag<string | undefined>;
filter: F.IOptionFlag<string[] | undefined>;
filter: F.IOptionFlag<string | undefined>;
csv: F.IFlag<boolean>;
output: F.IOptionFlag<string | undefined>;
extended: F.IFlag<boolean>;
Expand All @@ -300,7 +298,7 @@ export namespace table {
} = {
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', multiple: true}),
filter: F.string({description: 'filter property by partial string matching, ex: name=foo'}),
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 @@ -348,7 +346,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
14 changes: 3 additions & 11 deletions test/styled/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,16 @@ 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`)
})

fancy
.stdout()
.end('filters by multiple properties & values', output => {
cli.table(apps, {...columns, sid: {header: 'SID', get: (r: any) => r.stack && r.stack.id}}, {filter: ['id=\\d2\\d', '-sid=321']})
expect(output.stdout).to.equal(`ID Name${ws.padEnd(12)} SID${ws}
123 supertable-test-1 123${ws}\n`)
})

fancy
.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 @@ -276,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

0 comments on commit c008a42

Please sign in to comment.