Skip to content

Commit

Permalink
feat: add ctx.txt (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix authored Jan 23, 2025
1 parent ef8b40b commit 68f8cc2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,15 @@ export class Context implements RenamedUpdate {
this.callbackQuery?.message
);
}
/**
* Get the message text from wherever possible. Alias for `this.msg?.text ??
* this.msg?.caption`.
*/
get txt(): string | undefined {
// Keep in sync with types in `filter.ts`.
const msg = this.msg;
return msg?.text ?? msg?.caption;
}
/**
* Get the chat object from wherever possible. Alias for `(this.msg ??
* this.deletedBusinessMessages ?? this.messageReaction ??
Expand Down
3 changes: 3 additions & 0 deletions src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ interface Shortcuts<U extends Update> {
: [U["callback_query"]] extends [object]
? U["callback_query"]["message"]
: undefined;
txt: [Shortcuts<U>["msg"]] extends [{ text: string } | { caption: string }]
? string
: undefined;
chat: [U["callback_query"]] extends [object]
? NonNullable<U["callback_query"]["message"]>["chat"] | undefined
: [Shortcuts<U>["msg"]] extends [object] ? Shortcuts<U>["msg"]["chat"]
Expand Down
15 changes: 15 additions & 0 deletions test/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ describe("Context", () => {
ctx = new Context(up, api, me);
assertEquals(ctx.msg, up.edited_business_message);
});
it(".txt should aggregate text", () => {
let up: Update, ctx: Context;
const mc = {
caption: "b",
from: u,
chat: c,
sender_chat: c,
} as Message;
up = { message: update.message } as Update;
ctx = new Context(up, api, me);
assertEquals(ctx.txt, up.message?.text);
up = { message: mc } as Update;
ctx = new Context(up, api, me);
assertEquals(ctx.txt, up.message?.caption);
});

it(".chat should aggregate chats", () => {
let up: Update, ctx: Context;
Expand Down
6 changes: 5 additions & 1 deletion test/deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export {
} from "jsr:@std/assert";
export { afterEach, beforeEach, describe, it } from "jsr:@std/testing/bdd";
export { type Spy, spy, type Stub, stub } from "jsr:@std/testing/mock";
export { assertType, type IsExact } from "jsr:@std/testing/types";
export {
assertType,
type IsExact,
type IsNullable,
} from "jsr:@std/testing/types";
export { type IsMutuallyAssignable } from "jsr:@std/testing/unstable-types";

/**
Expand Down
23 changes: 23 additions & 0 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
assert,
assertEquals,
assertThrows,
assertType,
describe,
type IsNullable,
it,
} from "./deps.test.ts";

Expand Down Expand Up @@ -143,4 +145,25 @@ describe("matchFilter", () => {
throw "never";
}
});

it("should narrow down context shortcut types", () => {
const ctx = new Context(
// deno-lint-ignore no-explicit-any
{} as any,
// deno-lint-ignore no-explicit-any
undefined as any,
// deno-lint-ignore no-explicit-any
undefined as any,
);
if (matchFilter([":text", ":caption"])(ctx)) {
assertType<IsNullable<typeof ctx["msg"]>>(false);
assertType<IsNullable<typeof ctx["txt"]>>(false);
assertType<IsNullable<typeof ctx["chat"]>>(false);
assertType<IsNullable<typeof ctx["msgId"]>>(false);
assertType<IsNullable<typeof ctx["chatId"]>>(false);
}
if (matchFilter("message")(ctx)) {
assertType<IsNullable<typeof ctx["from"]>>(false);
}
});
});

0 comments on commit 68f8cc2

Please sign in to comment.