Skip to content

Commit

Permalink
fix: Ensure property filter prefers case-sensitive match (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
gethinwebster authored Dec 14, 2022
1 parent deb99d9 commit 3bd8f6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/property-filter/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ describe('matchFilteringProperty', () => {
const property = matchFilteringProperty(filteringProperties, ' Averange latency');
expect(property).toBe(null);
});
test('should prefer an exact match to non-exact', () => {
const properties: FilteringProperty[] = [
{ key: 'Test', propertyLabel: 'Test', groupValuesLabel: '' },
{ key: 'test', propertyLabel: 'test', groupValuesLabel: '' },
];
const property = matchFilteringProperty(properties, 'test');
expect(property).toBe(properties[1]);
});
});

describe('matchOperator', () => {
Expand Down
11 changes: 8 additions & 3 deletions src/property-filter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ export function matchFilteringProperty(
filteringProperties: readonly FilteringProperty[],
filteringText: string
): null | FilteringProperty {
filteringText = filteringText.toLowerCase();

let maxLength = 0;
let matchedProperty: null | FilteringProperty = null;

for (const property of filteringProperties) {
if (property.propertyLabel.length > maxLength && startsWith(filteringText, property.propertyLabel.toLowerCase())) {
if (property.propertyLabel === filteringText) {
matchedProperty = property;
break;
}
if (
property.propertyLabel.length > maxLength &&
startsWith(filteringText.toLowerCase(), property.propertyLabel.toLowerCase())
) {
maxLength = property.propertyLabel.length;
matchedProperty = property;
}
Expand Down

0 comments on commit 3bd8f6e

Please sign in to comment.