mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix: Filter component - array contains comparison not correct when ignore case option set to true (#10012)
This commit is contained in:
@@ -137,6 +137,18 @@ function parseRegexPattern(pattern: string): RegExp {
|
|||||||
return regex;
|
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
|
// eslint-disable-next-line complexity
|
||||||
export function executeFilterCondition(
|
export function executeFilterCondition(
|
||||||
condition: FilterConditionValue,
|
condition: FilterConditionValue,
|
||||||
@@ -284,15 +296,9 @@ export function executeFilterCondition(
|
|||||||
|
|
||||||
switch (condition.operator.operation) {
|
switch (condition.operator.operation) {
|
||||||
case 'contains':
|
case 'contains':
|
||||||
if (ignoreCase && typeof rightValue === 'string') {
|
return arrayContainsValue(left, rightValue, ignoreCase);
|
||||||
rightValue = rightValue.toLocaleLowerCase();
|
|
||||||
}
|
|
||||||
return left.includes(rightValue);
|
|
||||||
case 'notContains':
|
case 'notContains':
|
||||||
if (ignoreCase && typeof rightValue === 'string') {
|
return !arrayContainsValue(left, rightValue, ignoreCase);
|
||||||
rightValue = rightValue.toLocaleLowerCase();
|
|
||||||
}
|
|
||||||
return !left.includes(rightValue);
|
|
||||||
case 'lengthEquals':
|
case 'lengthEquals':
|
||||||
return left.length === rightNumber;
|
return left.length === rightNumber;
|
||||||
case 'lengthNotEquals':
|
case 'lengthNotEquals':
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { executeFilter } from '@/NodeParameters/FilterParameter';
|
import { arrayContainsValue, executeFilter } from '@/NodeParameters/FilterParameter';
|
||||||
import type { FilterConditionValue, FilterValue } from '@/Interfaces';
|
import type { FilterConditionValue, FilterValue } from '@/Interfaces';
|
||||||
import merge from 'lodash/merge';
|
import merge from 'lodash/merge';
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
@@ -1253,6 +1253,39 @@ describe('FilterParameter', () => {
|
|||||||
expect(result).toBe(expected);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user