fix: Filter component - array contains comparison not correct when ignore case option set to true (#10012)

This commit is contained in:
Michael Kret 2024-07-11 17:06:07 +03:00 committed by GitHub
parent 1aae65dfdc
commit 4a3b97cede
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 9 deletions

View file

@ -137,6 +137,18 @@ function parseRegexPattern(pattern: string): RegExp {
return regex;
}
export function arrayContainsValue(array: unknown[], value: unknown, ignoreCase: boolean): boolean {
if (ignoreCase && typeof value === 'string') {
return array.some((item) => {
if (typeof item !== 'string') {
return false;
}
return item.toString().toLocaleLowerCase() === value.toLocaleLowerCase();
});
}
return array.includes(value);
}
// eslint-disable-next-line complexity
export function executeFilterCondition(
condition: FilterConditionValue,
@ -284,15 +296,9 @@ export function executeFilterCondition(
switch (condition.operator.operation) {
case 'contains':
if (ignoreCase && typeof rightValue === 'string') {
rightValue = rightValue.toLocaleLowerCase();
}
return left.includes(rightValue);
return arrayContainsValue(left, rightValue, ignoreCase);
case 'notContains':
if (ignoreCase && typeof rightValue === 'string') {
rightValue = rightValue.toLocaleLowerCase();
}
return !left.includes(rightValue);
return !arrayContainsValue(left, rightValue, ignoreCase);
case 'lengthEquals':
return left.length === rightNumber;
case 'lengthNotEquals':

View file

@ -1,4 +1,4 @@
import { executeFilter } from '@/NodeParameters/FilterParameter';
import { arrayContainsValue, executeFilter } from '@/NodeParameters/FilterParameter';
import type { FilterConditionValue, FilterValue } from '@/Interfaces';
import merge from 'lodash/merge';
import { DateTime } from 'luxon';
@ -1253,6 +1253,39 @@ describe('FilterParameter', () => {
expect(result).toBe(expected);
});
});
describe('arrayContainsValue', () => {
test('should return true if the array contains the value', () => {
expect(arrayContainsValue([1, 2, 3], 2, false)).toBe(true);
});
test('should return false if the array does not contain the value', () => {
expect(arrayContainsValue([1, 2, 3], 4, false)).toBe(false);
});
test('should return true if the array contains the string value and ignoreCase is true', () => {
expect(arrayContainsValue(['a', 'b', 'c'], 'A', true)).toBe(true);
});
test('should return false if the array contains the string value but ignoreCase is false', () => {
expect(arrayContainsValue(['a', 'b', 'c'], 'A', false)).toBe(false);
});
test('should return false if the array does not contain the string value and ignoreCase is true', () => {
expect(arrayContainsValue(['a', 'b', 'c'], 'd', true)).toBe(false);
});
test('should handle non-string values correctly when ignoreCase is true', () => {
expect(arrayContainsValue([1, 2, 3], 2, true)).toBe(true);
expect(arrayContainsValue([1, 2, 3], '2', true)).toBe(false);
});
test('should handle mixed types in the array correctly', () => {
expect(arrayContainsValue(['a', 2, 'c'], 'A', true)).toBe(true);
expect(arrayContainsValue(['a', 2, 'c'], 2, false)).toBe(true);
expect(arrayContainsValue(['a', 2, 'c'], '2', false)).toBe(false);
});
});
});
});
});