Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead [nfc]: Dissolve `query_matches_source_attrs`.

Greg Pricec746d5669f78
diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js
index e2cba7cb110..710553ca0d9 100644
--- a/static/js/composebox_typeahead.js
+++ b/static/js/composebox_typeahead.js
@@ -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, " ");
};
}
diff --git a/static/shared/js/typeahead.js b/static/shared/js/typeahead.js
index 1898d840aa2..c992034e065 100644
--- a/static/shared/js/typeahead.js
+++ b/static/shared/js/typeahead.js
@@ -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, "_");
};
}
diff --git a/static/shared/js/typeahead.js.flow b/static/shared/js/typeahead.js.flow
index 04238362f51..cb5f5412e4f 100644
--- a/static/shared/js/typeahead.js.flow
+++ b/static/shared/js/typeahead.js.flow
@@ -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