Skip to content

Commit

Permalink
🚸 Fallback string comparison in string filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Slavetinsky committed Aug 8, 2023
1 parent be66b6e commit fb0a43f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function matchString(value: string, ref: string) {
try {
return new RegExp(ref).test(value);
} catch (error) {
throw new Error(`Invalid regular expression: ${ref}`);
return value.includes(ref);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/types/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as datatypes from '../datatypes';
import { isString } from '../datatypes';
import { notifyError } from '../notify';
import type { DataColumn, TableData } from './dataset';
import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator';

Expand Down Expand Up @@ -30,6 +32,19 @@ export class PredicateFilter<T = any> extends Filter {
this.column = column;
this.predicate = predicate;
this.referenceValue = referenceValue;

if (isString(column.type)) {
// as this is a string column try to create a regex from the reference value
// in order to be able to inform the user when this fails and we are likely going to
// fallback to a simple string comparison
try {
new RegExp(referenceValue as string);
} catch (e) {
notifyError(
`Couldn't create regex from filter value ${referenceValue}. Falling back to string comparison.`
);
}
}
}

get type(): datatypes.DataType {
Expand Down

0 comments on commit fb0a43f

Please sign in to comment.