Skip to content

Commit

Permalink
Merge pull request #191 from Renumics/frontend/bug/dont-fail-on-regexp
Browse files Browse the repository at this point in the history
🐛 dont fail when filter match fails instead remove filter and show message
  • Loading branch information
neindochoh authored Aug 8, 2023
2 parents 0a2132b + fb0a43f commit 9672bb0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ function matchString(value: string, ref: string) {
if (!ref?.length) {
return !value;
}
return new RegExp(ref).test(value);
try {
return new RegExp(ref).test(value);
} catch (error) {
return value.includes(ref);
}
}

const predicatesByType: PredicateRegistry = {
Expand Down
18 changes: 15 additions & 3 deletions src/stores/dataset/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
TableData,
} from '../../types';
import api from '../../api';
import { notifyAPIError } from '../../notify';
import { notifyAPIError, notifyError } from '../../notify';
import { makeColumnsColorTransferFunctions } from './colorTransferFunctionFactory';
import { makeColumn } from './columnFactory';
import { makeColumnsStats } from './statisticsFactory';
Expand Down Expand Up @@ -490,8 +490,20 @@ useDataset.subscribe(
};

const isIndexFiltered = Array(length);
for (let i = 0; i < length; i++) {
isIndexFiltered[i] = filters.every((filter) => applyFilter(filter, i));
try {
for (let i = 0; i < length; i++) {
isIndexFiltered[i] = filters.every((filter) => {
try {
return applyFilter(filter, i);
} catch (error) {
useDataset.getState().removeFilter(filter);
throw error;
}
});
}
} catch (error) {
console.error(error);
notifyError(`Error applying filter! '${error}'`);
}
const filteredIndices: number[] = [];
isIndexFiltered.forEach((isFiltered, i) => {
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 9672bb0

Please sign in to comment.