From eb09669fde3200592af3cb18ff7e91772bd0be9c Mon Sep 17 00:00:00 2001 From: codershiba <155646804+codershiba@users.noreply.github.com> Date: Sun, 7 Jan 2024 14:08:05 +0530 Subject: [PATCH 1/3] feat: add support for name param and object in `formatEmoji()` --- packages/discord.js/src/structures/Emoji.js | 2 +- .../formatters/__tests__/formatters.test.ts | 34 +++++++++ packages/formatters/src/formatters.ts | 74 +++++++++++++++++-- 3 files changed, 104 insertions(+), 6 deletions(-) diff --git a/packages/discord.js/src/structures/Emoji.js b/packages/discord.js/src/structures/Emoji.js index 1be75179b54b..f4c93e757c04 100644 --- a/packages/discord.js/src/structures/Emoji.js +++ b/packages/discord.js/src/structures/Emoji.js @@ -98,7 +98,7 @@ class Emoji extends Base { * reaction.message.channel.send(`The emoji used was: ${reaction.emoji}`); */ toString() { - return this.id ? formatEmoji(this.id, this.animated) : this.name; + return this.id ? formatEmoji({ animated: this.animated, id: this.id, name: this.name }) : this.name; } toJSON() { diff --git a/packages/formatters/__tests__/formatters.test.ts b/packages/formatters/__tests__/formatters.test.ts index b8b4a1e6217e..0feec150ce05 100644 --- a/packages/formatters/__tests__/formatters.test.ts +++ b/packages/formatters/__tests__/formatters.test.ts @@ -176,6 +176,40 @@ describe('Message formatters', () => { test('GIVEN animated emojiId THEN returns ""', () => { expect<``>(formatEmoji('827220205352255549', true)).toEqual(''); }); + + test('GIVEN static id in options object THEN returns "<:_:${id}>"', () => { + expect<`<:_:851461487498493952>`>(formatEmoji({ id: '851461487498493952' })).toEqual('<:_:851461487498493952>'); + }); + + test('GIVEN static id in options object WITH animated explicitly false THEN returns "<:_:${id}>"', () => { + expect<`<:_:851461487498493952>`>(formatEmoji({ animated: false, id: '851461487498493952' })).toEqual( + '<:_:851461487498493952>', + ); + }); + + test('GIVEN animated id in options object THEN returns ""', () => { + expect<``>(formatEmoji({ animated: true, id: '827220205352255549' })).toEqual( + '', + ); + }); + + test('GIVEN static id and name in options object THEN returns "<:${name}:${id}>"', () => { + expect<`<:test:851461487498493952>`>(formatEmoji({ id: '851461487498493952', name: 'test' })).toEqual( + '<:test:851461487498493952>', + ); + }); + + test('GIVEN static id and name WITH animated explicitly false THEN returns "<:${name}:${id}>"', () => { + expect<`<:test:851461487498493952>`>( + formatEmoji({ animated: false, id: '851461487498493952', name: 'test' }), + ).toEqual('<:test:851461487498493952>'); + }); + + test('GIVEN animated id and name THEN returns ""', () => { + expect<``>( + formatEmoji({ id: '827220205352255549', name: 'test', animated: true }), + ).toEqual(''); + }); }); describe('channelLink', () => { diff --git a/packages/formatters/src/formatters.ts b/packages/formatters/src/formatters.ts index 12d6dbac9e28..c53c7a7fe91f 100644 --- a/packages/formatters/src/formatters.ts +++ b/packages/formatters/src/formatters.ts @@ -336,11 +336,75 @@ export function formatEmoji( animated?: boolean, ): `<:_:${EmojiId}>` | ``; -export function formatEmoji( - emojiId: EmojiId, - animated = false, -): `<:_:${EmojiId}>` | `` { - return `<${animated ? 'a' : ''}:_:${emojiId}>`; +/** + * Formats a non-animated emoji id and name into a fully qualified emoji identifier. + * + * @typeParam EmojiId - This is inferred by the supplied emoji id + * @typeParam EmojiName - This is inferred by the supplied name + * @param options - The options for formatting an emoji + */ +export function formatEmoji( + options: Omit, 'animated'> & { animated?: false }, +): `<:${EmojiName}:${EmojiId}>`; + +/** + * Formats an animated emoji id and name into a fully qualified emoji identifier. + * + * @typeParam EmojiId - This is inferred by the supplied emoji id + * @typeParam EmojiName - This is inferred by the supplied name + * @param options - The options for formatting an emoji + */ +export function formatEmoji( + options: Omit, 'animated'> & { animated?: false }, +): ``; + +/** + * Formats an emoji id and name into a fully qualified emoji identifier. + * + * @typeParam EmojiId - This is inferred by the supplied emoji id + * @typeParam EmojiName - This is inferred by the supplied emoji name + * @param options - The options for formatting an emoji + */ +export function formatEmoji( + options: FormatEmojiOptions, +): `<:${EmojiName}:${EmojiId}>` | ``; + +export function formatEmoji( + emojiIdOrOptions: EmojiId | FormatEmojiOptions, + animated?: boolean, +): `<:${string}:${EmojiId}>` | `` { + const options = + typeof emojiIdOrOptions === 'string' + ? { + id: emojiIdOrOptions, + animated: animated ?? false, + } + : emojiIdOrOptions; + + const { id, animated: isAnimated, name: emojiName } = options; + + return `<${isAnimated ? 'a' : ''}:${emojiName ?? '_'}:${id}>`; +} + +/** + * The options for formatting an emoji. + * + * @typeParam EmojiId - This is inferred by the supplied emoji id + * @typeParam EmojiName - This is inferred by the supplied emoji name + */ +export interface FormatEmojiOptions { + /** + * Whether the emoji is animated + */ + animated?: boolean; + /** + * The emoji id to format + */ + id: EmojiId; + /** + * The name of the emoji + */ + name?: EmojiName; } /** From d30d7e800f1a2cc09f5094835630b3678c782497 Mon Sep 17 00:00:00 2001 From: codershiba <155646804+codershiba@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:56:39 +0530 Subject: [PATCH 2/3] Update formatters.ts --- packages/formatters/src/formatters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/formatters/src/formatters.ts b/packages/formatters/src/formatters.ts index c53c7a7fe91f..2b027134ff52 100644 --- a/packages/formatters/src/formatters.ts +++ b/packages/formatters/src/formatters.ts @@ -355,7 +355,7 @@ export function formatEmoji * @param options - The options for formatting an emoji */ export function formatEmoji( - options: Omit, 'animated'> & { animated?: false }, + options: Omit, 'animated'> & { animated?: true }, ): ``; /** From 3f0cfcefac5f9d98c95a36ea3ee86eaf73115a0c Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:51:56 +0000 Subject: [PATCH 3/3] refactor: swap priority --- packages/formatters/src/formatters.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/formatters/src/formatters.ts b/packages/formatters/src/formatters.ts index 2b027134ff52..a33820369c98 100644 --- a/packages/formatters/src/formatters.ts +++ b/packages/formatters/src/formatters.ts @@ -344,8 +344,8 @@ export function formatEmoji( * @param options - The options for formatting an emoji */ export function formatEmoji( - options: Omit, 'animated'> & { animated?: false }, -): `<:${EmojiName}:${EmojiId}>`; + options: FormatEmojiOptions & { animated: true }, +): ``; /** * Formats an animated emoji id and name into a fully qualified emoji identifier. @@ -355,8 +355,8 @@ export function formatEmoji * @param options - The options for formatting an emoji */ export function formatEmoji( - options: Omit, 'animated'> & { animated?: true }, -): ``; + options: FormatEmojiOptions & { animated?: false }, +): `<:${EmojiName}:${EmojiId}>`; /** * Formats an emoji id and name into a fully qualified emoji identifier.