feat: move internal regular expressions to static properties (#5384)

This commit is contained in:
Jan
2021-04-15 00:36:35 +02:00
committed by GitHub
parent 900e57657e
commit 207735cedc
4 changed files with 19 additions and 4 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}
/**

2
typings/index.d.ts vendored
View File

@@ -906,6 +906,7 @@ declare module 'discord.js' {
public delete(): Promise<GuildTemplate>;
public edit(options?: { name?: string; description?: string }): Promise<GuildTemplate>;
public sync(): Promise<GuildTemplate>;
public static GUILD_TEMPLATES_PATTERN: RegExp;
}
export class GuildPreviewEmoji extends BaseGuildEmoji {
@@ -978,6 +979,7 @@ declare module 'discord.js' {
public delete(reason?: string): Promise<Invite>;
public toJSON(): object;
public toString(): string;
public static INVITES_PATTERN: RegExp;
}
export class Message extends Base {