@grammyjs/commands 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/command.d.ts CHANGED
@@ -2,7 +2,7 @@ import { type BotCommand, type BotCommandScope, type BotCommandScopeAllChatAdmin
2
2
  import type { CommandOptions } from "./types.js";
3
3
  import { type MaybeArray } from "./utils.js";
4
4
  type BotCommandGroupsScope = BotCommandScopeAllGroupChats | BotCommandScopeAllChatAdministrators;
5
- export declare const matchesPattern: (value: string, pattern: string | RegExp) => boolean;
5
+ export declare const matchesPattern: (value: string, pattern: string | RegExp, ignoreCase?: boolean) => boolean;
6
6
  /**
7
7
  * Class that represents a single command and allows you to configure it.
8
8
  */
package/out/command.js CHANGED
@@ -7,7 +7,15 @@ const utils_js_1 = require("./utils.js");
7
7
  const isAdmin = (ctx) => ctx
8
8
  .getAuthor()
9
9
  .then((author) => ["administrator", "creator"].includes(author.status));
10
- const matchesPattern = (value, pattern) => typeof pattern === "string" ? value === pattern : pattern.test(value);
10
+ const matchesPattern = (value, pattern, ignoreCase = false) => {
11
+ const transformedValue = ignoreCase ? value.toLowerCase() : value;
12
+ const transformedPattern = pattern instanceof RegExp && ignoreCase && !pattern.flags.includes("i")
13
+ ? new RegExp(pattern, pattern.flags + "i")
14
+ : pattern;
15
+ return typeof transformedPattern === "string"
16
+ ? transformedValue === transformedPattern
17
+ : transformedPattern.test(transformedValue);
18
+ };
11
19
  exports.matchesPattern = matchesPattern;
12
20
  /**
13
21
  * Class that represents a single command and allows you to configure it.
@@ -30,6 +38,7 @@ class Command {
30
38
  prefix: "/",
31
39
  matchOnlyAtStart: true,
32
40
  targetedCommands: "optional",
41
+ ignoreCase: false,
33
42
  };
34
43
  this._options = { ...this._options, ...options };
35
44
  if (this._options.prefix === "")
@@ -160,7 +169,7 @@ class Command {
160
169
  continue;
161
170
  if (username && username !== ctx.me.username)
162
171
  continue;
163
- if (commandNames.some((name) => (0, exports.matchesPattern)(command.replace(prefix, ""), name))) {
172
+ if (commandNames.some((name) => (0, exports.matchesPattern)(command.replace(prefix, ""), name, options.ignoreCase))) {
164
173
  return true;
165
174
  }
166
175
  }
package/out/commands.d.ts CHANGED
@@ -33,11 +33,11 @@ export declare class Commands<C extends Context> {
33
33
  private _languages;
34
34
  private _scopes;
35
35
  private _commands;
36
- private _composer;
36
+ private _cachedComposer;
37
+ private _cachedComposerInvalidated;
37
38
  private _commandOptions;
38
39
  constructor(options?: Partial<CommandOptions>);
39
40
  private _addCommandToScope;
40
- private _populateComposer;
41
41
  private _populateMetadata;
42
42
  /**
43
43
  * Registers a new command with a default handler.
package/out/commands.js CHANGED
@@ -35,7 +35,8 @@ class Commands {
35
35
  this._languages = new Set();
36
36
  this._scopes = new Map();
37
37
  this._commands = [];
38
- this._composer = new deps_node_js_1.Composer();
38
+ this._cachedComposer = new deps_node_js_1.Composer();
39
+ this._cachedComposerInvalidated = false;
39
40
  this._commandOptions = {};
40
41
  this._commandOptions = options;
41
42
  }
@@ -44,11 +45,6 @@ class Commands {
44
45
  const commands = (_a = this._scopes.get(JSON.stringify(scope))) !== null && _a !== void 0 ? _a : [];
45
46
  this._scopes.set(JSON.stringify(scope), commands.concat([command]));
46
47
  }
47
- _populateComposer() {
48
- for (const command of this._commands) {
49
- this._composer.use(command.middleware());
50
- }
51
- }
52
48
  _populateMetadata() {
53
49
  this._languages.clear();
54
50
  this._scopes.clear();
@@ -73,6 +69,7 @@ class Commands {
73
69
  if (handler)
74
70
  command.addToScope({ type: "default" }, handler);
75
71
  this._commands.push(command);
72
+ this._cachedComposerInvalidated = true;
76
73
  return command;
77
74
  }
78
75
  /**
@@ -176,11 +173,14 @@ class Commands {
176
173
  return JSON.stringify(this);
177
174
  }
178
175
  middleware() {
179
- this._populateComposer();
180
- return this._composer.middleware();
176
+ if (this._cachedComposerInvalidated) {
177
+ this._cachedComposer = new deps_node_js_1.Composer(...this._commands);
178
+ this._cachedComposerInvalidated = false;
179
+ }
180
+ return this._cachedComposer.middleware();
181
181
  }
182
182
  /**
183
- * Replaces the `toString` method on node.js
183
+ * Replaces the `toString` method on Deno
184
184
  *
185
185
  * @see toString
186
186
  */
@@ -188,7 +188,7 @@ class Commands {
188
188
  return this.toString();
189
189
  }
190
190
  /**
191
- * Replaces the `toString` method on Deno
191
+ * Replaces the `toString` method on Node.js
192
192
  *
193
193
  * @see toString
194
194
  */
package/out/types.d.ts CHANGED
@@ -22,6 +22,11 @@ export interface CommandOptions {
22
22
  * - `"required"`: only targeted commands are matched
23
23
  */
24
24
  targetedCommands: "ignored" | "optional" | "required";
25
+ /**
26
+ * Whether match against commands in a case-insensitive manner.
27
+ * Defaults to `false`.
28
+ */
29
+ ignoreCase: boolean;
25
30
  }
26
31
  export interface CommandElementals {
27
32
  name: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grammyjs/commands",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "grammY Commands Plugin",
5
5
  "main": "out/mod.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "ts-pattern": "^5.0.1"
22
22
  },
23
23
  "devDependencies": {
24
+ "deno-bin": "^1.45.2",
24
25
  "typescript": "^5.1.6"
25
26
  },
26
27
  "files": [