From 207735cedcf9a998571a328c7c7b2414d3ebe9d5 Mon Sep 17 00:00:00 2001 From: Jan <66554238+vaporox@users.noreply.github.com> Date: Thu, 15 Apr 2021 00:36:35 +0200 Subject: [PATCH] feat: move internal regular expressions to static properties (#5384) --- src/structures/GuildTemplate.js | 6 ++++++ src/structures/Invite.js | 6 ++++++ src/util/DataResolver.js | 9 +++++---- typings/index.d.ts | 2 ++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/structures/GuildTemplate.js b/src/structures/GuildTemplate.js index 7510bb959..4285d8bb9 100644 --- a/src/structures/GuildTemplate.js +++ b/src/structures/GuildTemplate.js @@ -222,4 +222,10 @@ class GuildTemplate extends Base { } } +/** + * Regular expression that globally matches guild template links + * @type {RegExp} + */ +GuildTemplate.GUILD_TEMPLATES_PATTERN = /discord(?:app)?\.(?:com\/template|new)\/([\w-]{2,255})/gi; + module.exports = GuildTemplate; diff --git a/src/structures/Invite.js b/src/structures/Invite.js index 683326685..f252253a6 100644 --- a/src/structures/Invite.js +++ b/src/structures/Invite.js @@ -191,4 +191,10 @@ class Invite extends Base { } } +/** + * Regular expression that globally matches Discord invite links + * @type {RegExp} + */ +Invite.INVITES_PATTERN = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/gi; + module.exports = Invite; diff --git a/src/util/DataResolver.js b/src/util/DataResolver.js index 84a92ed72..36f837808 100644 --- a/src/util/DataResolver.js +++ b/src/util/DataResolver.js @@ -5,6 +5,8 @@ const path = require('path'); const stream = require('stream'); const fetch = require('node-fetch'); const { Error: DiscordError, TypeError } = require('../errors'); +const GuildTemplate = require('../structures/GuildTemplate'); +const Invite = require('../structures/Invite'); /** * The DataResolver identifies different objects and tries to resolve a specific piece of information from them. @@ -36,8 +38,7 @@ class DataResolver { * @returns {string} */ static resolveCode(data, regex) { - const match = regex.exec(data); - return match ? match[1] || data : data; + return data.matchAll(regex).next().value?.[1] ?? data; } /** @@ -46,7 +47,7 @@ class DataResolver { * @returns {string} */ static resolveInviteCode(data) { - return this.resolveCode(data, /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/i); + return this.resolveCode(data, Invite.INVITES_PATTERN); } /** @@ -55,7 +56,7 @@ class DataResolver { * @returns {string} */ static resolveGuildTemplateCode(data) { - return this.resolveCode(data, /discord(?:app)?\.(?:com\/template|new)\/([\w-]{2,255})/i); + return this.resolveCode(data, GuildTemplate.GUILD_TEMPLATES_PATTERN); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index af822d726..8075e77bf 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -906,6 +906,7 @@ declare module 'discord.js' { public delete(): Promise; public edit(options?: { name?: string; description?: string }): Promise; public sync(): Promise; + public static GUILD_TEMPLATES_PATTERN: RegExp; } export class GuildPreviewEmoji extends BaseGuildEmoji { @@ -978,6 +979,7 @@ declare module 'discord.js' { public delete(reason?: string): Promise; public toJSON(): object; public toString(): string; + public static INVITES_PATTERN: RegExp; } export class Message extends Base {