regex helper
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 18s

This commit is contained in:
Mylloon 2024-09-27 15:50:55 +02:00
parent 5279c63fa2
commit b20365db13
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 68 additions and 5 deletions

View file

@ -2,6 +2,7 @@ import { Client, EmbedBuilder, Message, TextBasedChannel } from "discord.js";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { isImage, userWithNickname } from "../../utils/misc"; import { isImage, userWithNickname } from "../../utils/misc";
import { showDate } from "../../utils/time"; import { showDate } from "../../utils/time";
import { RegexC, RegExpFlags } from "../../utils/regex";
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate */ /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-messageCreate */
export default async (message: Message, client: Client) => { export default async (message: Message, client: Client) => {
@ -24,7 +25,7 @@ export default async (message: Message, client: Client) => {
/* Citation */ /* Citation */
const regex = const regex =
/https:\/\/(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(\d{17,19})\/(\d{17,19})\/(\d{17,19})/g; /https:\/\/(?:canary\.|ptb\.)?discord(?:app)?\.com\/channels\/(\d{17,19})\/(\d{17,19})\/(\d{17,19})/g;
const urls = message.content.match(new RegExp(regex, "g")); const urls = message.content.match(RegexC(regex, RegExpFlags.Global));
// Ignore message if there is no URLs // Ignore message if there is no URLs
if (!urls) { if (!urls) {
@ -42,7 +43,7 @@ export default async (message: Message, client: Client) => {
}[] = [], }[] = [],
match, match,
) => { ) => {
const [, guild_id, channel_id, message_id] = new RegExp(regex).exec( const [, guild_id, channel_id, message_id] = RegexC(regex).exec(
match, match,
) as RegExpExecArray; ) as RegExpExecArray;
@ -178,7 +179,7 @@ export default async (message: Message, client: Client) => {
// Delete source message if no content when removing links // Delete source message if no content when removing links
if ( if (
!message.content.replace(new RegExp(regex, "g"), "").trim() && !message.content.replace(RegexC(regex, RegExpFlags.Global), "").trim() &&
messages.length === urls.length && messages.length === urls.length &&
!message.mentions.repliedUser && !message.mentions.repliedUser &&
message.channel.isSendable() message.channel.isSendable()

View file

@ -0,0 +1,26 @@
import { RegexC, RegExpFlags } from "../../utils/regex";
describe("Regex flags", () => {
test("One parameter", () => {
const regex = RegexC("", RegExpFlags.Global);
expect(regex.global).toBeTruthy();
});
test("All parameters", () => {
const regex = RegexC(
"",
RegExpFlags.Global |
RegExpFlags.MultiLine |
RegExpFlags.Insensitive |
RegExpFlags.Sticky |
RegExpFlags.Unicode |
RegExpFlags.SingleLine,
);
expect(regex.global).toBeTruthy();
expect(regex.multiline).toBeTruthy();
expect(regex.ignoreCase).toBeTruthy();
expect(regex.sticky).toBeTruthy();
expect(regex.unicode).toBeTruthy();
expect(regex.dotAll).toBeTruthy();
});
});

30
src/utils/regex.ts Normal file
View file

@ -0,0 +1,30 @@
export enum RegExpFlags {
// Global
Global = 1 << 0,
// Multi Line
MultiLine = 1 << 1,
// Ignore Case
Insensitive = 1 << 2,
// Sticky
Sticky = 1 << 3,
// Unicode
Unicode = 1 << 4,
// Dot All
SingleLine = 1 << 6,
}
const flagsToString = (flags: number) => {
let result = "";
if (flags & RegExpFlags.Global) result += "g";
if (flags & RegExpFlags.MultiLine) result += "m";
if (flags & RegExpFlags.Insensitive) result += "i";
if (flags & RegExpFlags.Sticky) result += "y";
if (flags & RegExpFlags.Unicode) result += "u";
if (flags & RegExpFlags.SingleLine) result += "s";
return result;
};
export const RegexC = (pattern: RegExp | string, flags: number = 0) =>
new RegExp(pattern, flagsToString(flags));

View file

@ -2,6 +2,7 @@ import { Client, Colors, EmbedBuilder, User } from "discord.js";
import { getLocale } from "./locales"; import { getLocale } from "./locales";
import { cleanCodeBlock } from "./misc"; import { cleanCodeBlock } from "./misc";
import { showDate, strToSeconds, timeDeltaToString } from "./time"; import { showDate, strToSeconds, timeDeltaToString } from "./time";
import { RegexC, RegExpFlags } from "./regex";
/** /**
* Option possible for reminders * Option possible for reminders
@ -52,7 +53,10 @@ export const splitTime = (time: string) => {
}; };
const lowered = time.toLowerCase(); const lowered = time.toLowerCase();
const trimmed = lowered.replaceAll(new RegExp(Object.values(mapping).join("|"), "g"), ""); const trimmed = lowered.replaceAll(
RegexC(Object.values(mapping).join("|"), RegExpFlags.Global),
"",
);
// Depending of the last character of the string // Depending of the last character of the string
switch (lowered.slice(-1)) { switch (lowered.slice(-1)) {

View file

@ -1,4 +1,5 @@
import moment from "moment-timezone"; import moment from "moment-timezone";
import { RegexC, RegExpFlags } from "./regex";
/** /**
* Parsed string adapted with TZ (locales) and format for the specified lang * Parsed string adapted with TZ (locales) and format for the specified lang
@ -41,7 +42,7 @@ export const strToSeconds = (time: string) => {
return -1; return -1;
} }
const regex = new RegExp( const regex = RegexC(
`(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${ `(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${
TimeSecond[TimeSecond.Week] TimeSecond[TimeSecond.Week]
}>[0-9]+(?=[w]))|(?<${TimeSecond[TimeSecond.Day]}>[0-9]+(?=[d|j]))|(?<${ }>[0-9]+(?=[w]))|(?<${TimeSecond[TimeSecond.Day]}>[0-9]+(?=[d|j]))|(?<${
@ -49,6 +50,7 @@ export const strToSeconds = (time: string) => {
}>[0-9]+(?=[h]))|(?<${TimeSecond[TimeSecond.Minute]}>[0-9]+(?=[m]))|(?<${ }>[0-9]+(?=[h]))|(?<${TimeSecond[TimeSecond.Minute]}>[0-9]+(?=[m]))|(?<${
TimeSecond[TimeSecond.Second] TimeSecond[TimeSecond.Second]
}>[0-9]+(?=[s]?))`, }>[0-9]+(?=[s]?))`,
RegExpFlags.Global | RegExpFlags.Insensitive,
); );
const data = Object.assign({}, regex.exec(time.toLowerCase())?.groups); const data = Object.assign({}, regex.exec(time.toLowerCase())?.groups);