Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
filter: Return operator's operand type in NarrowCanonicalTerm.
Aman Agrawalcb1b01cacf39
diff --git a/web/src/filter.ts b/web/src/filter.tsindex 8df3e4ce306..8c4eb58f3b7 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -1031,8 +1031,13 @@ export class Filter {return safe_to_return;}- terms_with_operator(operator: NarrowCanonicalOperator): NarrowTerm[] {- return this._terms.filter((term) => !term.negated && term.operator === operator);+ terms_with_operator<T extends NarrowCanonicalTerm["operator"]>(+ operator: T,+ ): Extract<NarrowCanonicalTerm, {operator: T}>[] {+ return this._terms.filter(+ (term): term is Extract<NarrowCanonicalTerm, {operator: T}> =>+ !term.negated && term.operator === operator,+ );}has_negated_operand(operator: string, operand: string): boolean {diff --git a/web/src/state_data.ts b/web/src/state_data.tsindex 2b753881762..491f0cf560b 100644--- a/web/src/state_data.ts+++ b/web/src/state_data.ts@@ -53,11 +53,78 @@ export const narrow_operator_schema = z.union([]);export type NarrowOperator = z.output<typeof narrow_operator_schema>;-export const narrow_canonical_term_schema = z.object({- negated: z.optional(z.boolean()),- operator: narrow_canonical_operator_schema,- operand: z.string(),-});+export const narrow_canonical_term_schema = z.discriminatedUnion("operator", [+ z.object({+ operator: z.literal(""),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("channel"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("channels"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("has"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("id"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("in"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("is"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("near"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("search"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("topic"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("with"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("sender"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("dm-including"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+ z.object({+ operator: z.literal("dm"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),+]);export type NarrowCanonicalTerm = z.output<typeof narrow_canonical_term_schema>;export const narrow_term_schema = z.union([diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjsindex e616eeab85c..a69743a88ae 100644--- a/web/tests/filter.test.cjs+++ b/web/tests/filter.test.cjs@@ -3143,6 +3143,6 @@ run_test("get_stringified_narrow_for_server_query", () => {const narrow = filter.get_stringified_narrow_for_server_query();assert.equal(narrow,- '[{"negated":false,"operator":"channel","operand":1},{"negated":false,"operator":"topic","operand":"bar"}]',+ '[{"operator":"channel","operand":1,"negated":false},{"operator":"topic","operand":"bar","negated":false}]',);});