people: Add dm_matches_search_string for DM filter.

This function does not respect `,` (commas) in the search term and will
treat a comma as any other character. We can decide how to treat comma
separated terms in future iterations. That is also the reason that we
introduce this 2 line function instead of just using the person matcher
directly in future commits. This function still supports search terms
with diacritics because of using person matcher.
This commit is contained in:
Shubham Padia
2024-07-03 17:19:55 +00:00
committed by Tim Abbott
parent b5804f2185
commit 188dd87eec
2 changed files with 47 additions and 0 deletions

View File

@@ -1209,6 +1209,12 @@ export function filter_people_by_search_terms(users: User[], search_string: stri
return filtered_users;
}
export function dm_matches_search_string(users: User[], search_string: string): boolean {
const matcher = build_person_matcher(search_string);
return users.some((user) => matcher(user));
}
export const is_valid_full_name_and_user_id = (full_name: string, user_id: number): boolean => {
/*
This function is currently only used for checking

View File

@@ -757,6 +757,47 @@ test_people("filtered_users", () => {
assert.ok(filtered_people.has(noah.user_id));
});
test_people("dm_matches_search_string", () => {
people.add_active_user(charles);
people.add_active_user(maria);
people.add_active_user(ashton);
people.add_active_user(linus);
people.add_active_user(noah);
people.add_active_user(plain_noah);
let result = people.dm_matches_search_string([ashton], "a");
assert.ok(result);
// Maria's email starts with `a`.
result = people.dm_matches_search_string([maria], "a");
assert.ok(result);
// Neither Charles' full name or email start with `a`.
result = people.dm_matches_search_string([charles], "a");
assert.ok(!result);
// Empty search terms should always return true
result = people.dm_matches_search_string([charles], "");
assert.ok(result);
result = people.dm_matches_search_string([charles, maria, noah], "");
assert.ok(result);
// Match with email.
result = people.dm_matches_search_string([linus], "ltorv");
assert.ok(result);
// Test filtering of names with diacritics. This should match
// Nöôáàh by ignoring diacritics, and also match Nooaah.
result = people.dm_matches_search_string([noah], "noOa");
assert.ok(result);
result = people.dm_matches_search_string([plain_noah], "noOa");
assert.ok(result);
// This should match ëmerson, but not emerson.
result = people.dm_matches_search_string([noah], "ëm");
assert.ok(result);
result = people.dm_matches_search_string([plain_noah], "ëm");
assert.ok(!result);
});
test_people("multi_user_methods", () => {
people.add_active_user(emp401);
people.add_active_user(emp402);