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

Commit d75b31b

Browse files
author
Milad Imen
authored
Revert "Revert "feat: support multiple filter flags for table (#156)" (#231)"
This reverts commit c008a42.
1 parent c008a42 commit d75b31b

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/styled/table.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,20 @@ class Table<T extends object> {
5959
})
6060

6161
// filter rows
62-
if (this.options.filter) {
63-
/* eslint-disable-next-line prefer-const */
64-
let [header, regex] = this.options.filter!.split('=')
65-
const isNot = header[0] === '-'
66-
if (isNot) header = header.substr(1)
67-
const col = this.findColumnFromHeader(header)
68-
if (!col || !regex) throw new Error('Filter flag has an invalid value')
69-
rows = rows.filter((d: any) => {
70-
const re = new RegExp(regex)
71-
const val = d[col!.key]
72-
const match = val.match(re)
73-
return isNot ? !match : match
62+
if (!_.isEmpty(this.options.filter)) {
63+
this.options.filter!.forEach(filter => {
64+
/* eslint-disable-next-line prefer-const */
65+
let [header, regex] = filter.split('=')
66+
const isNot = header[0] === '-'
67+
if (isNot) header = header.substr(1)
68+
const col = this.findColumnFromHeader(header)
69+
if (!col || !regex) throw new Error('Filter flag has an invalid value')
70+
rows = rows.filter((d: any) => {
71+
const re = new RegExp(regex)
72+
const val = d[col!.key]
73+
const match = re.test(val)
74+
return isNot ? !match : match
75+
})
7476
})
7577
}
7678

@@ -289,7 +291,7 @@ export namespace table {
289291
export const Flags: {
290292
columns: F.IOptionFlag<string | undefined>;
291293
sort: F.IOptionFlag<string | undefined>;
292-
filter: F.IOptionFlag<string | undefined>;
294+
filter: F.IOptionFlag<string[] | undefined>;
293295
csv: F.IFlag<boolean>;
294296
output: F.IOptionFlag<string | undefined>;
295297
extended: F.IFlag<boolean>;
@@ -298,7 +300,7 @@ export namespace table {
298300
} = {
299301
columns: F.string({exclusive: ['extended'], description: 'only show provided columns (comma-separated)'}),
300302
sort: F.string({description: 'property to sort by (prepend \'-\' for descending)'}),
301-
filter: F.string({description: 'filter property by partial string matching, ex: name=foo'}),
303+
filter: F.string({description: 'filter property by partial string matching, ex: name=foo', multiple: true}),
302304
csv: F.boolean({exclusive: ['no-truncate'], description: 'output is csv format [alias: --output=csv]'}),
303305
output: F.string({
304306
exclusive: ['no-truncate', 'csv'],
@@ -346,7 +348,7 @@ export namespace table {
346348
export interface Options {
347349
[key: string]: any;
348350
sort?: string;
349-
filter?: string;
351+
filter?: string[];
350352
columns?: string;
351353
extended?: boolean;
352354
'no-truncate'?: boolean;

test/styled/table.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,24 @@ describe('styled/table', () => {
233233
fancy
234234
.stdout()
235235
.end('filters by property & value (partial string match)', output => {
236-
cli.table(apps, columns, {filter: 'id=123'})
236+
cli.table(apps, columns, {filter: ['id=123']})
237237
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
238238
123 supertable-test-1${ws}\n`)
239239
})
240240

241+
fancy
242+
.stdout()
243+
.end('filters by multiple properties & values', output => {
244+
cli.table(apps, {...columns, sid: {header: 'SID', get: (r: any) => r.stack && r.stack.id}}, {filter: ['id=\\d2\\d', '-sid=321']})
245+
expect(output.stdout).to.equal(`ID Name${ws.padEnd(12)} SID${ws}
246+
123 supertable-test-1 123${ws}\n`)
247+
})
248+
241249
fancy
242250
.stdout()
243251
.end('does not truncate', output => {
244252
const three = {...apps[0], id: '0'.repeat(80), name: 'supertable-test-3'}
245-
cli.table(apps.concat(three), columns, {filter: 'id=0', 'no-truncate': true})
253+
cli.table(apps.concat(three), columns, {filter: ['id=0'], 'no-truncate': true})
246254
expect(output.stdout).to.equal(`ID${ws.padEnd(78)} Name${ws.padEnd(14)}
247255
${three.id} supertable-test-3${ws}\n`)
248256
})
@@ -268,7 +276,7 @@ ${three.id} supertable-test-3${ws}\n`)
268276
fancy
269277
.stdout()
270278
.end('ignores header case', output => {
271-
cli.table(apps, columns, {columns: 'iD,Name', filter: 'nAMe=supertable-test', sort: '-ID'})
279+
cli.table(apps, columns, {columns: 'iD,Name', filter: ['nAMe=supertable-test'], sort: '-ID'})
272280
expect(output.stdout).to.equal(`ID Name${ws.padEnd(14)}
273281
321 supertable-test-2${ws}
274282
123 supertable-test-1${ws}\n`)

0 commit comments

Comments
 (0)