From c8895b22d76eb016e0459bacbb9b321be449c5ae Mon Sep 17 00:00:00 2001 From: Arnulfo Solis Date: Wed, 26 Feb 2020 15:15:32 +0100 Subject: [PATCH 1/2] allow multiple filter flags --- src/styled/table.ts | 28 +++++++++++++++------------- test/styled/table.test.ts | 6 +++--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index 5b954271..dee57cd7 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -58,18 +58,20 @@ class Table { }) // filter rows - if (this.options.filter) { + if (this.options.filter && !_.isEmpty(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 + this.options.filter.forEach(filter => { + 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 + }) }) } @@ -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'], @@ -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; diff --git a/test/styled/table.test.ts b/test/styled/table.test.ts index 1d03fa0f..3fecb349 100644 --- a/test/styled/table.test.ts +++ b/test/styled/table.test.ts @@ -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`) }) @@ -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`) }) @@ -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`) From 8fec520fe5117af3035b9df9e91e42a4c8485abb Mon Sep 17 00:00:00 2001 From: Arnulfo Solis Date: Wed, 26 Feb 2020 15:29:04 +0100 Subject: [PATCH 2/2] fixed lint --- src/styled/table.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index dee57cd7..a98cd93f 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -59,8 +59,8 @@ class Table { // filter rows if (this.options.filter && !_.isEmpty(this.options.filter)) { - /* eslint-disable-next-line prefer-const */ 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)