Merge pull request #13349 from backstage/freben/matchers

Properly handle free-text entity filtering in the case of empty tag arrays
This commit is contained in:
Fredrik Adelöw
2022-08-25 14:28:54 +02:00
committed by GitHub
2 changed files with 30 additions and 13 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-react': patch
---
Properly handle free-text entity filtering in the case of empty tag arrays
+25 -13
View File
@@ -84,20 +84,32 @@ export class EntityTextFilter implements EntityFilter {
constructor(readonly value: string) {}
filterEntity(entity: Entity): boolean {
const upperCaseValue = this.value.toLocaleUpperCase('en-US');
const words = this.toUpperArray(this.value.split(/\s/));
const exactMatch = this.toUpperArray([entity.metadata.tags]);
const partialMatch = this.toUpperArray([
entity.metadata.name,
entity.metadata.title,
]);
return (
entity.metadata.name
.toLocaleUpperCase('en-US')
.includes(upperCaseValue) ||
`${entity.metadata.title}`
.toLocaleUpperCase('en-US')
.includes(upperCaseValue) ||
entity.metadata.tags
?.join('')
.toLocaleUpperCase('en-US')
.indexOf(upperCaseValue) !== -1
);
for (const word of words) {
if (
exactMatch.every(m => m !== word) &&
partialMatch.every(m => !m.includes(word))
) {
return false;
}
}
return true;
}
private toUpperArray(
value: Array<string | string[] | undefined>,
): Array<string> {
return value
.flat()
.filter((m): m is string => Boolean(m))
.map(m => m.toLocaleUpperCase('en-US'));
}
}