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.

util: Implement a safer type narrowing helper for arrays.

Zixuan James Liee26590528ca
diff --git a/web/src/people.js b/web/src/people.js
index 24da8bb5df1..68667f27ace 100644
--- a/web/src/people.js
+++ b/web/src/people.js
@@ -209,12 +209,14 @@ export function huddle_string(message) {
export function user_ids_string_to_emails_string(user_ids_string) {
const user_ids = split_to_ints(user_ids_string);
- let emails = user_ids.map((user_id) => {
- const person = people_by_user_id_dict.get(user_id);
- return person && person.email;
- });
+ let emails = util.try_parse_as_truthy(
+ user_ids.map((user_id) => {
+ const person = people_by_user_id_dict.get(user_id);
+ return person && person.email;
+ }),
+ );
- if (!emails.every(Boolean)) {
+ if (emails === undefined) {
blueslip.warn("Unknown user ids: " + user_ids_string);
return undefined;
}
@@ -261,12 +263,14 @@ export function reply_to_to_user_ids_string(emails_string) {
// invalid data.
const emails = emails_string.split(",");
- let user_ids = emails.map((email) => {
- const person = get_by_email(email);
- return person && person.user_id;
- });
+ let user_ids = util.try_parse_as_truthy(
+ emails.map((email) => {
+ const person = get_by_email(email);
+ return person && person.user_id;
+ }),
+ );
- if (!user_ids.every(Boolean)) {
+ if (user_ids === undefined) {
return undefined;
}
@@ -322,12 +326,14 @@ export function emails_strings_to_user_ids_string(emails_string) {
}
export function email_list_to_user_ids_string(emails) {
- let user_ids = emails.map((email) => {
- const person = get_by_email(email);
- return person && person.user_id;
- });
+ let user_ids = util.try_parse_as_truthy(
+ emails.map((email) => {
+ const person = get_by_email(email);
+ return person && person.user_id;
+ }),
+ );
- if (!user_ids.every(Boolean)) {
+ if (user_ids === undefined) {
blueslip.warn("Unknown emails: " + emails);
return undefined;
}
@@ -564,9 +570,9 @@ export function update_email_in_reply_to(reply_to, user_id, new_email) {
// and we don't warn on any errors.
let emails = reply_to.split(",");
- const persons = emails.map((email) => people_dict.get(email.trim()));
+ const persons = util.try_parse_as_truthy(emails.map((email) => people_dict.get(email.trim())));
- if (!persons.every(Boolean)) {
+ if (persons === undefined) {
return reply_to;
}
@@ -589,7 +595,11 @@ export function update_email_in_reply_to(reply_to, user_id, new_email) {
export function pm_with_operand_ids(operand) {
let emails = operand.split(",");
emails = emails.map((email) => email.trim());
- let persons = emails.map((email) => people_dict.get(email));
+ let persons = util.try_parse_as_truthy(emails.map((email) => people_dict.get(email)));
+
+ if (persons === undefined) {
+ return undefined;
+ }
// If your email is included in a PM group with other people, just ignore it
if (persons.length > 1) {
@@ -597,10 +607,6 @@ export function pm_with_operand_ids(operand) {
persons = persons.filter((person) => person !== my_user);
}
- if (!persons.every(Boolean)) {
- return undefined;
- }
-
let user_ids = persons.map((person) => person.user_id);
user_ids = sort_numerically(user_ids);
@@ -1002,10 +1008,12 @@ export function get_message_people() {
at the message_store code to see the precise
semantics
*/
- const message_people = message_user_ids
- .user_ids()
- .map((user_id) => people_by_user_id_dict.get(user_id))
- .filter(Boolean);
+ const message_people = util.try_parse_as_truthy(
+ message_user_ids
+ .user_ids()
+ .map((user_id) => people_by_user_id_dict.get(user_id))
+ .filter(Boolean),
+ );
return message_people;
}
diff --git a/web/src/util.ts b/web/src/util.ts
index 594ceac58c6..c8e750684a7 100644
--- a/web/src/util.ts
+++ b/web/src/util.ts
@@ -439,3 +439,22 @@ export function get_string_diff(string1: string, string2: string): [number, numb
return [diff_start_index, diff_end_1_index, diff_end_2_index];
}
+
+export function try_parse_as_truthy<T>(val: (T | undefined)[]): T[] | undefined {
+ // This is a typesafe helper to narrow an array from containing
+ // possibly falsy values into an array containing non-undefined
+ // items or undefined when any of the items is falsy.
+
+ // While this eliminates the possibility of returning an array
+ // with falsy values, the type annotation does not provide that
+ // guarantee. Ruling out undefined values is sufficient for the
+ // helper's usecases.
+ const result: T[] = [];
+ for (const x of val) {
+ if (!x) {
+ return undefined;
+ }
+ result.push(x);
+ }
+ return result;
+}