Skip to content

Commit

Permalink
Refactor filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Simon committed Nov 1, 2019
1 parent e1edab4 commit f2729a8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import withFixedColumns from "react-table-hoc-fixed-columns";
import "react-table-hoc-fixed-columns/lib/styles.css";
import "react-table/react-table.css";
import {
filterByRegex,
applyFilter,
sortMethod,
pathOr,
pipe,
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class Table extends React.Component {
/>
);
},
filterMethod: filterByRegex,
filterMethod: applyFilter,
Filter: ({ filter, onChange }) => {
let value;
const type = column.type._type._type
Expand Down
14 changes: 7 additions & 7 deletions benchexec/tablegenerator/react-table/src/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (C) Dirk Beyer. All rights reserved.
*/
import {
filterByRegex,
applyFilter,
isOkStatus,
pipe,
maybeTransformToLowercase,
Expand Down Expand Up @@ -49,11 +49,11 @@ const rows = [

//Function to test filtering by regex for data set 'rows' (return number of truely returnd values)
const getFilteredData = regex =>
rows.filter(row => filterByRegex({ id: "test", value: regex }, row));
rows.filter(row => applyFilter({ id: "test", value: regex }, row));

test("filterByRegex single entry without result", () => {
test("applyFilter single entry without result", () => {
expect(
filterByRegex(
applyFilter(
{
id: "test",
value: "10:"
Expand All @@ -70,15 +70,15 @@ test("filterByRegex single entry without result", () => {

//use function getFilteredData to generate test cases with data set 'rows'

test("filterByRegex greater 10", () => {
test("applyFilter greater 10", () => {
expect(getFilteredData("10:").length).toBe(4);
});

test("filterByRegex equals 10", () => {
test("applyFilter equals 10", () => {
expect(getFilteredData("10").length).toBe(2);
});

test("filterByRegex between 10.3 and 10.7", () => {
test("applyFilter between 10.3 and 10.7", () => {
expect(getFilteredData("10.3:10.7").length).toBe(1);
});

Expand Down
35 changes: 16 additions & 19 deletions benchexec/tablegenerator/react-table/src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ const prepareTableData = ({ head, tools, rows, stats, props }) => {
};
};

const filterByRegex = (filter, row, cell) => {
const pattern = /((-?\d*\.?\d*):(-?\d*\.?\d*))|(-?\d*\.?\d*)/;
const applyFilter = (filter, row, cell) => {
const { original } = row[filter.id];
const filterParams = filter.value.split(":");

const regex = filter.value.match(pattern);
if (regex[2] === undefined) {
return String(row[filter.id].formatted).startsWith(filter.value);
} else if (!regex[3]) {
if (+row[filter.id].original >= Number(regex[2])) {
return row[filter.id];
}
} else if (!regex[2]) {
if (+row[filter.id].original <= Number(regex[3])) {
return row[filter.id];
}
} else if (
row[filter.id].original >= Number(regex[2]) &&
row[filter.id].original <= Number(regex[3])
) {
return row[filter.id];
if (filterParams.length === 2) {
const [start, end] = filterParams;

const numOriginal = Number(original);
const numStart = Number(start);
const numEnd = end ? Number(end) : Infinity;

return numOriginal >= numStart && numOriginal <= numEnd;
}

if (filterParams.length === 1) {
return original.startsWith(filterParams[0]);
}
return false;
};
Expand Down Expand Up @@ -83,7 +80,7 @@ const isOkStatus = status => {

export {
prepareTableData,
filterByRegex,
applyFilter,
sortMethod,
isOkStatus,
pathOr,
Expand Down

0 comments on commit f2729a8

Please sign in to comment.