Skip to content

Commit

Permalink
feat: move internal regular expressions to static properties (#5384)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporoxx authored Apr 14, 2021
1 parent 900e576 commit 207735c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/structures/GuildTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions src/structures/Invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 5 additions & 4 deletions src/util/DataResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

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

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

/**
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 207735c

Please sign in to comment.