From fc54f9a03b974e000cde4ea4a18ddb261572f003 Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Wed, 1 Apr 2020 23:45:13 -0400 Subject: [PATCH] fix(filter): string filter should also work when using Contains - when using OperatorType.empty or OperatorType.contains, they should do a comparison of passing when item contains the substring of search term --- .../__tests__/stringFilterCondition.spec.ts | 6 ++++++ .../common/src/filter-conditions/stringFilterCondition.ts | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/common/src/filter-conditions/__tests__/stringFilterCondition.spec.ts b/packages/common/src/filter-conditions/__tests__/stringFilterCondition.spec.ts index 9b90009de..f68e7a57c 100644 --- a/packages/common/src/filter-conditions/__tests__/stringFilterCondition.spec.ts +++ b/packages/common/src/filter-conditions/__tests__/stringFilterCondition.spec.ts @@ -70,6 +70,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); diff --git a/packages/common/src/filter-conditions/stringFilterCondition.ts b/packages/common/src/filter-conditions/stringFilterCondition.ts index 8a968e952..b976d0ee6 100644 --- a/packages/common/src/filter-conditions/stringFilterCondition.ts +++ b/packages/common/src/filter-conditions/stringFilterCondition.ts @@ -1,5 +1,6 @@ import { FilterCondition, FilterConditionOption } from '../interfaces/index'; import { testFilterCondition } from './filterUtilities'; +import { OperatorType } from '../enums'; export const stringFilterCondition: FilterCondition = (options: FilterConditionOption) => { // make sure the cell value is a string by casting it when possible @@ -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);