Skip to content

Commit

Permalink
Merge pull request #368 from ghiscoding/bugfix/filter-in-contains-tri…
Browse files Browse the repository at this point in the history
…m-spaces

fix(filters): filtering with IN_CONTAINS should also work with spaces
  • Loading branch information
AnnetteZhang authored Jun 2, 2021
2 parents 4b979dd + ab54724 commit fbab810
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ describe('filterUtilities', () => {
expect(output).toBeTruthy();
});

it('should return True when value1 is "IN_CONTAINS" value2 collection even if there is extra spaces in the string', () => {
const output = testFilterCondition('IN_CONTAINS', 'Task2, Task3 ', ['Task2', 'Task3']);
expect(output).toBeTruthy();
});

it('should return False when value1 is not "IN_CONTAINS" value2 collection', () => {
const output = testFilterCondition('IN_CONTAINS', 'Task11,Task4', ['Task 1', 'Task2', 'Task3']);
expect(output).toBeFalsy();
Expand All @@ -212,7 +217,15 @@ describe('filterUtilities', () => {
expect(output1).toBeFalsy();
expect(output2).toBeFalsy();
});


it('should return False when value1 is not "NOT_IN_CONTAINS" value2 collection even if there is extra spaces in the string', () => {
const output1 = testFilterCondition('NIN_CONTAINS', 'Task2, Task3 ', ['Task2', 'Task3']);
const output2 = testFilterCondition('NOT_IN_CONTAINS', 'Task2, Task3', ['Task2', 'Task3']);

expect(output1).toBeFalsy();
expect(output2).toBeFalsy();
});

it('should return False when value2 is not a collection', () => {
const output = testFilterCondition('NOT_IN_CONTAINS', 'Task2,Task3', 'Task2');
expect(output).toBeFalsy();
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/filter-conditions/filterUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ export const testFilterCondition = (operator: OperatorString, value1: any, value
return ((value2 && Array.isArray(value2 as string[])) ? (!value2.includes(value1)) : false);
case 'IN_CONTAINS':
if (value2 && Array.isArray(value2) && typeof value1 === 'string') {
return value2.some(item => value1.split(/[\s,]+/).includes(item));
return value2.some(item => value1.split(/[\s,]+/).map(val => (val.trim())).includes(item));
}
return false;
case 'NIN_CONTAINS':
case 'NOT_IN_CONTAINS':
if (value2 && Array.isArray(value2) && typeof value1 === 'string') {
return !value2.some(item => value1.split(/[\s,]+/).includes(item));
return !value2.some(item => value1.split(/[\s,]+/).map(val => (val.trim())).includes(item));
}
return false;
case 'IN_COLLECTION':
Expand Down

0 comments on commit fbab810

Please sign in to comment.