From bae831d5ce84846cb9671e144e459e1032f97066 Mon Sep 17 00:00:00 2001 From: Andrey Azov Date: Tue, 18 Jun 2024 10:08:56 +0100 Subject: [PATCH] Change method for case-insensitive substring check (#1146) --- .../selectable-genomes-table/filterGenomes.ts | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/content/app/species-selector/components/selectable-genomes-table/filterGenomes.ts b/src/content/app/species-selector/components/selectable-genomes-table/filterGenomes.ts index 510b771b27..0608a16227 100644 --- a/src/content/app/species-selector/components/selectable-genomes-table/filterGenomes.ts +++ b/src/content/app/species-selector/components/selectable-genomes-table/filterGenomes.ts @@ -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;