Skip to content

Commit

Permalink
add insensitiveness support for regex, add test's for regex's
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix committed Jul 19, 2024
1 parent 90b1d8c commit 71d5da2
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 58 deletions.
11 changes: 7 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ export const matchesPattern = (
ignoreCase = false,
) => {
const transformedValue = ignoreCase ? value.toLowerCase() : value;

return typeof pattern === "string"
? transformedValue === pattern
: pattern.test(transformedValue);
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);
};

/**
Expand Down
185 changes: 131 additions & 54 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,64 +385,141 @@ describe("Command", () => {

describe("ignoreCase", () => {
describe("true", () => {
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";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
assert(
Command.hasCommand("start", {
...options,
ignoreCase: true,
})(ctx),
);
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", () => {
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";
m.entities = [{
type: "bot_command",
offset: 0,
length: 6,
}];
assert(
Command.hasCommand("start", {
...options,
ignoreCase: false,
})(ctx),
);
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),
);
});
});
});
});
Expand Down

0 comments on commit 71d5da2

Please sign in to comment.