Skip to content

Commit

Permalink
fix(filter): string filter should also work when using Contains (#427)
Browse files Browse the repository at this point in the history
- when using OperatorType.empty or OperatorType.contains, they should do a comparison of passing when item contains the substring of search term

Co-authored-by: Ghislain Beaulac <ghislain.beaulac@se.com>
  • Loading branch information
ghiscoding and ghiscoding-SE committed Apr 3, 2020
1 parent 0c47038 commit 2c0765b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ describe('stringFilterCondition method', () => {
expect(output).toBe(true);
});

it('should return True when search term is a substring of the cell value and the operator is Contains', () => {
const options = { dataKey: '', operator: 'Contains', cellValue: 'abbostford', fieldType: FieldType.string, searchTerms: ['bost'] } as FilterConditionOption;
const output = stringFilterCondition(options);
expect(output).toBe(true);
});

it('should return True when input value provided starts with same substring and the operator is empty string', () => {
const options = { dataKey: '', operator: '', cellValue: 'abbostford', fieldType: FieldType.string, searchTerms: ['abb'] } as FilterConditionOption;
const output = stringFilterCondition(options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FilterCondition, FilterConditionOption } from '../models/index';
import { testFilterCondition } from './filterUtilities';
import { OperatorType } from '../models';

export const stringFilterCondition: FilterCondition = (options: FilterConditionOption) => {
// make sure the cell value is a string by casting it when possible
Expand All @@ -12,11 +13,11 @@ export const stringFilterCondition: FilterCondition = (options: FilterConditionO
searchTerm = searchTerm.toLowerCase();
}

if (options.operator === '*' || options.operator === 'EndsWith') {
if (options.operator === '*' || options.operator === OperatorType.endsWith) {
return cellValue.endsWith(searchTerm);
} else if ((options.operator === '' && options.cellValueLastChar === '*') || options.operator === 'StartsWith') {
} else if ((options.operator === '' && options.cellValueLastChar === '*') || options.operator === OperatorType.startsWith) {
return cellValue.startsWith(searchTerm);
} else if (options.operator === '') {
} else if (options.operator === '' || options.operator === OperatorType.contains) {
return (cellValue.indexOf(searchTerm) > -1);
}
return testFilterCondition(options.operator || '==', cellValue, searchTerm);
Expand Down

0 comments on commit 2c0765b

Please sign in to comment.