Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow command casing to be ignored #30

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"deno.enable": true,
"deno.unstable": true,
"deno.config": "./deno.jsonc",
"editor.defaultFormatter": "denoland.vscode-deno"
"editor.defaultFormatter": "denoland.vscode-deno",
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
10 changes: 9 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@
"fix": "deno lint --fix && deno fmt",
"test": "deno test --seed=123456 --parallel ./test/",
"coverage": "rm -rf ./test/cov_profile && deno task test --coverage=./test/cov_profile && deno coverage --lcov --output=./coverage.lcov ./test/cov_profile"
}
},
"exclude": [
"node_modules",
"test/cov_profile",
".vscode",
"coverage.lcov",
"out",
"src/deps.node.ts"
]
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grammyjs/commands",
"version": "0.8.1",
"version": "0.9.0",
"description": "grammY Commands Plugin",
"main": "out/mod.js",
"scripts": {
Expand Down
23 changes: 20 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ const isAdmin = (ctx: Context) =>
.getAuthor()
.then((author) => ["administrator", "creator"].includes(author.status));

export const matchesPattern = (value: string, pattern: string | RegExp) =>
typeof pattern === "string" ? value === pattern : pattern.test(value);
export const matchesPattern = (
value: string,
pattern: string | RegExp,
ignoreCase = false,
) => {
const transformedValue = ignoreCase ? value.toLowerCase() : value;
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved
const transformedPattern =
pattern instanceof RegExp && ignoreCase && !pattern.flags.includes("i")
? new RegExp(pattern, pattern.flags + "i")
: pattern;
return typeof transformedPattern === "string"
? transformedValue === transformedPattern
: transformedPattern.test(transformedValue);
};

/**
* Class that represents a single command and allows you to configure it.
Expand All @@ -41,6 +53,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
prefix: "/",
matchOnlyAtStart: true,
targetedCommands: "optional",
ignoreCase: false,
};

/**
Expand Down Expand Up @@ -241,7 +254,11 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
if (username && username !== ctx.me.username) continue;
if (
commandNames.some((name) =>
matchesPattern(command.replace(prefix, ""), name)
matchesPattern(
command.replace(prefix, ""),
roziscoding marked this conversation as resolved.
Show resolved Hide resolved
name,
options.ignoreCase,
)
)
) {
return true;
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export interface CommandOptions {
* - `"required"`: only targeted commands are matched
*/
targetedCommands: "ignored" | "optional" | "required";
/**
* Whether match against commands in a case-insensitive manner.
* Defaults to `false`.
*/
ignoreCase: boolean;
}

export interface CommandElementals {
Expand Down
142 changes: 142 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe("Command", () => {
matchOnlyAtStart: true,
prefix: "/",
targetedCommands: "optional",
ignoreCase: false,
};

describe("hasCommand", () => {
Expand Down Expand Up @@ -381,6 +382,147 @@ describe("Command", () => {
});
});
});

describe("ignoreCase", () => {
describe("true", () => {
describe("for string commands", () => {
it("should match a command in a case-insensitive manner", () => {
m.text = "/START";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
const ctx = new Context(update, api, me);
assert(
Command.hasCommand("start", {
...options,
ignoreCase: true,
})(ctx),
);
m.text = "/start";
assert(
Command.hasCommand("start", {
...options,
ignoreCase: true,
})(ctx),
);
});
});
describe("for regex commands", () => {
it("should match a command in a case-insensitive manner", () => {
m.text = "/START";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
const ctx = new Context(update, api, me);
assert(
Command.hasCommand(/start/, {
...options,
ignoreCase: true,
})(ctx),
);
assert(
Command.hasCommand(/start/i, {
...options,
ignoreCase: true,
})(ctx),
);
m.text = "/start";
assert(
Command.hasCommand(/sTaRt/, {
...options,
ignoreCase: true,
})(ctx),
);
assert(
Command.hasCommand(/sTaRt/i, {
...options,
ignoreCase: true,
})(ctx),
);
});
});
});

describe("false", () => {
describe("for string commands", () => {
it("should match a command in a case-sensitive manner", () => {
m.text = "/START";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
const ctx = new Context(update, api, me);
assertFalse(
Command.hasCommand("start", {
...options,
ignoreCase: false,
})(ctx),
);

m.text = "/start";
assert(
Command.hasCommand("start", {
...options,
ignoreCase: false,
})(ctx),
);
});
});
describe("for regex commands", () => {
describe("should match a command in a case-sensitive manner", () => {
it("under normal conditions", () => {
m.text = "/START";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
const ctx = new Context(update, api, me);
assertFalse(
Command.hasCommand(/start/, {
...options,
ignoreCase: false,
})(ctx),
);
m.text = "/start";
assertFalse(
Command.hasCommand(/START/, {
...options,
ignoreCase: false,
})(ctx),
);
m.text = "/start";
assert(
Command.hasCommand(/start/, {
...options,
ignoreCase: false,
})(ctx),
);
});
});
it("should prioritize the `i` flag even if ignoreCase is set to false", () => {
m.text = "/START";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
const ctx = new Context(update, api, me);
assert(
Command.hasCommand(/start/i, {
...options,
ignoreCase: false,
})(ctx),
);
});
});
});
});
});

describe("matchesPattern", () => {
Expand Down
Loading