Transformations - make substring and not substring transformer case insensitive. (#84699)

* make substring and not substring transformer case insensitive

* timlevett/case-insensitive/ run linter

---------

Co-authored-by: jev forsberg <jev.forsberg@grafana.com>
This commit is contained in:
Tim Levett
2024-04-03 11:11:09 -05:00
committed by GitHub
parent 7cfd470c91
commit a5707788ba
2 changed files with 25 additions and 3 deletions

View File

@ -9,7 +9,7 @@ describe('value substring to matcher', () => {
fields: [
{
name: 'temp',
values: ['24', null, '10', 'asd', '42'],
values: ['24', null, '10', 'asd', '42', 'ASD'],
},
],
}),
@ -30,6 +30,20 @@ describe('value substring to matcher', () => {
expect(matcher(valueIndex, field, frame, data)).toBeTruthy();
});
it('should match case insensitive', () => {
const frame = data[0];
const field = frame.fields[0];
const valueIndex = 5;
const caseInsensitiveMatcher = getValueMatcher({
id: ValueMatcherID.substring,
options: {
value: 'asd',
},
});
expect(caseInsensitiveMatcher(valueIndex, field, frame, data)).toBeTruthy();
});
// Added for https://github.com/grafana/grafana/pull/83548#pullrequestreview-1904931540 where the matcher was not handling null values
it('should be a mismatch if the option is null and should not cause errors', () => {
const frame = data[0];

View File

@ -11,7 +11,9 @@ const isSubstringMatcher: ValueMatcherInfo<BasicValueMatcherOptions> = {
get: (options) => {
return (valueIndex: number, field: Field) => {
const value = field.values[valueIndex];
return (value && value.includes(options.value)) || options.value === '';
return (
(value && options.value && value.toLowerCase().includes(options.value.toLowerCase())) || options.value === ''
);
};
},
getOptionsDisplayText: () => {
@ -28,7 +30,13 @@ const isNotSubstringValueMatcher: ValueMatcherInfo<BasicValueMatcherOptions> = {
get: (options) => {
return (valueIndex: number, field: Field) => {
const value = field.values[valueIndex];
return typeof value === 'string' && options.value !== '' && !value.includes(options.value);
return (
typeof value === 'string' &&
options.value &&
value &&
options.value !== '' &&
!value.toLowerCase().includes(options.value.toLowerCase())
);
};
},
getOptionsDisplayText: () => {