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.

left_sidebar: Switch topic filtering to any-order word prefix matching.

Pritesh Shetty7630a41857d5
diff --git a/web/src/topic_list_data.ts b/web/src/topic_list_data.ts
index c8743c57d32..11e6a7b83cf 100644
--- a/web/src/topic_list_data.ts
+++ b/web/src/topic_list_data.ts
@@ -4,6 +4,7 @@ import * as narrow_state from "./narrow_state.ts";
import * as resolved_topic from "./resolved_topic.ts";
import * as stream_topic_history from "./stream_topic_history.ts";
import * as sub_store from "./sub_store.ts";
+import * as typeahead from "./typeahead.ts";
import * as unread from "./unread.ts";
import * as user_topics from "./user_topics.ts";
import * as util from "./util.ts";
@@ -163,14 +164,16 @@ export function filter_topics_by_search_term(
return topic_names;
}
- const word_separator_regex = /[\s/:_-]/; // Use -, _, :, / as word separators in addition to spaces.
const empty_string_topic_display_name = util.get_final_topic_display_name("");
- topic_names = util.filter_by_word_prefix_match(
- topic_names,
- search_term,
- (topic) => (topic === "" ? empty_string_topic_display_name : topic),
- word_separator_regex,
- );
+ const normalize = (s: string): string => s.replaceAll(/[:/_-]+/g, " ");
+ const normalized_query = normalize(search_term);
+
+ topic_names = topic_names.filter((topic) => {
+ const topic_string = topic === "" ? empty_string_topic_display_name : topic;
+ const normalized_topic = normalize(topic_string);
+
+ return typeahead.query_matches_string_in_any_order(normalized_query, normalized_topic, " ");
+ });
if (topics_state === "is:resolved") {
topic_names = topic_names.filter((name) => resolved_topic.is_resolved(name));
diff --git a/web/tests/topic_list_data.test.cjs b/web/tests/topic_list_data.test.cjs
index 5965c31ec2c..e9c66f3f5de 100644
--- a/web/tests/topic_list_data.test.cjs
+++ b/web/tests/topic_list_data.test.cjs
@@ -218,18 +218,42 @@ test("get_list_info w/real stream_topic_history", ({override}) => {
assert.equal(list_info.more_topics_have_unread_mention_messages, false);
assert.equal(list_info.num_possible_topics, 11);
- add_topic_message("After Brooklyn", 1008);
- add_topic_message("Delhi", 1009);
+ add_topic_message("Backend Developer", 1008);
+ add_topic_message("Developer Backend", 1009);
// When topic search input is not empty, we show topics
// based on the search term.
- let search_term = "b,d";
+ let search_term = "b d";
list_info = get_list_info(zoomed, search_term);
assert.equal(list_info.items.length, 2);
assert.equal(list_info.more_topics_unreads, 0);
assert.equal(list_info.more_topics_have_unread_mention_messages, false);
assert.equal(list_info.num_possible_topics, 2);
+ add_topic_message("Work (Project A)", 1010);
+ search_term = "Work (";
+ list_info = get_list_info(zoomed, search_term);
+ assert.equal(list_info.items.length, 1);
+ assert.equal(list_info.items[0].topic_name, "Work (Project A)");
+
+ search_term = "BACKEND";
+ list_info = get_list_info(zoomed, search_term);
+ assert.equal(list_info.items.length, 2);
+ assert.equal(list_info.items[0].topic_name, "Developer Backend");
+ assert.equal(list_info.items[1].topic_name, "Backend Developer");
+
+ add_topic_message("authentication flow", 1011);
+ list_info = get_list_info(zoomed, "auth");
+ assert.equal(list_info.items.length, 1);
+ list_info = get_list_info(zoomed, "thent");
+ assert.equal(list_info.items.length, 0);
+
+ add_topic_message("backend deployment guide", 1012);
+ list_info = get_list_info(zoomed, "ba de gu");
+ assert.equal(list_info.items.length, 1);
+ list_info = get_list_info(zoomed, "gu ba de");
+ assert.equal(list_info.items.length, 1);
+
// Verify empty string topic shows up for "general" search term.
search_term = "general";
list_info = get_list_info(zoomed, search_term);