Skip to content

Commit

Permalink
Change method for case-insensitive substring check (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
azangru authored Jun 18, 2024
1 parent 5409775 commit bae831d
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,35 @@ const filterGenomes = ({
genomes: SpeciesSearchMatch[];
}) => {
return genomes.filter((genome) => {
const queryRegex = getQueryRegex(query);

return (
genome.common_name?.match(queryRegex) ||
genome.scientific_name.match(queryRegex) ||
genome.type?.kind.match(queryRegex) ||
genome.type?.value.match(queryRegex) ||
(genome.is_reference && 'reference'.match(queryRegex)) ||
genome.assembly.accession_id.match(queryRegex) ||
genome.assembly.name.match(queryRegex) ||
genome.annotation_provider.match(queryRegex) ||
genome.annotation_method.match(queryRegex)
isSubstringOf(genome.common_name, query) ||
isSubstringOf(genome.scientific_name, query) ||
isSubstringOf(genome.type?.kind, query) ||
isSubstringOf(genome.type?.value, query) ||
(genome.is_reference && isSubstringOf('reference', query)) ||
isSubstringOf(genome.assembly.accession_id, query) ||
isSubstringOf(genome.assembly.name, query) ||
isSubstringOf(genome.assembly.name, query) ||
isSubstringOf(genome.annotation_provider, query) ||
isSubstringOf(genome.annotation_method, query)
);
});
};

const getQueryRegex = (query: string) => {
return new RegExp(query, 'i');
// Strings in modern javascript have a .localeCompare method
// that can do case-insensitive string comparison;
// but it does not check whether a string contains a substring.
// The function below is an old an not very elegant way of checking this.
const isSubstringOf = (
string: string | undefined | null,
candidateSubstring: string
) => {
if (typeof string !== 'string') {
return false;
}
const normalizedString = string.toUpperCase();
const normalizedCandidateString = candidateSubstring.toUpperCase();
return normalizedString.includes(normalizedCandidateString);
};

export default filterGenomes;

0 comments on commit bae831d

Please sign in to comment.