typeahead [nfc]: Dissolve query_matches_source_attrs.

This makes each of the call sites more straightforward and
transparent.  In `query_matches_person` it also opens up further
simplifications, which we'll make next.

Since we're now exporting `query_matches_string` from this
shared module, we write its type in the `.js.flow` file.
This commit is contained in:
Greg Price
2022-10-28 13:29:53 -07:00
committed by Tim Abbott
parent 7400165031
commit c746d5669f
3 changed files with 22 additions and 23 deletions

View File

@@ -87,17 +87,20 @@ function get_language_matcher(query) {
export function query_matches_person(query, person) {
if (!settings_data.show_email()) {
return typeahead.query_matches_source_attrs(query, person, ["full_name"], " ");
return typeahead.query_matches_string(query, person.full_name, " ");
}
let email_attr = "email";
if (person.delivery_email) {
email_attr = "delivery_email";
}
return typeahead.query_matches_source_attrs(query, person, ["full_name", email_attr], " ");
return (
typeahead.query_matches_string(query, person.full_name, " ") ||
typeahead.query_matches_string(query, person[email_attr], " ")
);
}
export function query_matches_name(query, user_group_or_stream) {
return typeahead.query_matches_source_attrs(query, user_group_or_stream, ["name"], " ");
return typeahead.query_matches_string(query, user_group_or_stream.name, " ");
}
function get_stream_or_user_group_matcher(query) {
@@ -113,7 +116,10 @@ function get_slash_matcher(query) {
query = typeahead.clean_query_lowercase(query);
return function (item) {
return typeahead.query_matches_source_attrs(query, item, ["name", "aliases"], " ");
return (
typeahead.query_matches_string(query, item.name, " ") ||
typeahead.query_matches_string(query, item.aliases, " ")
);
};
}
@@ -125,7 +131,7 @@ function get_topic_matcher(query) {
topic,
};
return typeahead.query_matches_source_attrs(query, obj, ["topic"], " ");
return typeahead.query_matches_string(query, obj.topic, " ");
};
}

View File

@@ -34,7 +34,11 @@ export function remove_diacritics(s) {
return s.normalize("NFKD").replace(unicode_marks, "");
}
function query_matches_string(query, source_str, split_char) {
// This function attempts to match a query with a source text.
// * query is the user-entered search query
// * source_str is the string we're matching in, e.g. a user's name
// * split_char is the separator for this syntax (e.g. ' ').
export function query_matches_string(query, source_str, split_char) {
source_str = source_str.toLowerCase();
source_str = remove_diacritics(source_str);
@@ -51,19 +55,6 @@ function query_matches_string(query, source_str, split_char) {
return source_str.startsWith(query) || source_str.includes(split_char + query);
}
// This function attempts to match a query with source's attributes.
// * query is the user-entered search query
// * Source is the object we're matching from, e.g. a user object
// * match_attrs are the values associated with the target object that
// the entered string might be trying to match, e.g. for a user
// account, there might be 2 attrs: their full name and their email.
// * split_char is the separator for this syntax (e.g. ' ').
export function query_matches_source_attrs(query, source, match_attrs, split_char) {
return match_attrs.some((attr) => {
return query_matches_string(query, source[attr], split_char);
});
}
function clean_query(query) {
query = remove_diacritics(query);
// When `abc ` with a space at the end is typed in a
@@ -98,9 +89,7 @@ export function get_emoji_matcher(query) {
return function (emoji) {
const matches_emoji_literal =
is_unicode_emoji(emoji) && parse_unicode_emoji_code(emoji.emoji_code) === query;
return (
matches_emoji_literal || query_matches_source_attrs(query, emoji, ["emoji_name"], "_")
);
return matches_emoji_literal || query_matches_string(query, emoji.emoji_name, "_");
};
}

View File

@@ -6,7 +6,11 @@ export type Emoji = {emoji_name: string, emoji_code: string, ...};
declare export function remove_diacritics(s: string): string;
// declare export function query_matches_source_attrs
declare export function query_matches_string(
query: string,
source_str: string,
split_char: string,
): boolean;
// declare export function clean_query_lowercase