Skip to content

Commit 7b8e0de

Browse files
codershibaJiralite
andauthored
refactor(formatters): Add support for object and name param in formatEmoji() (#10076)
* feat: add support for name param and object in `formatEmoji()` * Update formatters.ts * refactor: swap priority --------- Co-authored-by: Jiralite <[email protected]>
1 parent 136c66c commit 7b8e0de

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

packages/discord.js/src/structures/Emoji.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Emoji extends Base {
9898
* reaction.message.channel.send(`The emoji used was: ${reaction.emoji}`);
9999
*/
100100
toString() {
101-
return this.id ? formatEmoji(this.id, this.animated) : this.name;
101+
return this.id ? formatEmoji({ animated: this.animated, id: this.id, name: this.name }) : this.name;
102102
}
103103

104104
toJSON() {

packages/formatters/__tests__/formatters.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,40 @@ describe('Message formatters', () => {
176176
test('GIVEN animated emojiId THEN returns "<a:_:${emojiId}>"', () => {
177177
expect<`<a:_:827220205352255549>`>(formatEmoji('827220205352255549', true)).toEqual('<a:_:827220205352255549>');
178178
});
179+
180+
test('GIVEN static id in options object THEN returns "<:_:${id}>"', () => {
181+
expect<`<:_:851461487498493952>`>(formatEmoji({ id: '851461487498493952' })).toEqual('<:_:851461487498493952>');
182+
});
183+
184+
test('GIVEN static id in options object WITH animated explicitly false THEN returns "<:_:${id}>"', () => {
185+
expect<`<:_:851461487498493952>`>(formatEmoji({ animated: false, id: '851461487498493952' })).toEqual(
186+
'<:_:851461487498493952>',
187+
);
188+
});
189+
190+
test('GIVEN animated id in options object THEN returns "<a:_:${id}>"', () => {
191+
expect<`<a:_:827220205352255549>`>(formatEmoji({ animated: true, id: '827220205352255549' })).toEqual(
192+
'<a:_:827220205352255549>',
193+
);
194+
});
195+
196+
test('GIVEN static id and name in options object THEN returns "<:${name}:${id}>"', () => {
197+
expect<`<:test:851461487498493952>`>(formatEmoji({ id: '851461487498493952', name: 'test' })).toEqual(
198+
'<:test:851461487498493952>',
199+
);
200+
});
201+
202+
test('GIVEN static id and name WITH animated explicitly false THEN returns "<:${name}:${id}>"', () => {
203+
expect<`<:test:851461487498493952>`>(
204+
formatEmoji({ animated: false, id: '851461487498493952', name: 'test' }),
205+
).toEqual('<:test:851461487498493952>');
206+
});
207+
208+
test('GIVEN animated id and name THEN returns "<a:${name}:${id}>"', () => {
209+
expect<`<a:test:827220205352255549>`>(
210+
formatEmoji({ id: '827220205352255549', name: 'test', animated: true }),
211+
).toEqual('<a:test:827220205352255549>');
212+
});
179213
});
180214

181215
describe('channelLink', () => {

packages/formatters/src/formatters.ts

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,75 @@ export function formatEmoji<EmojiId extends Snowflake>(
336336
animated?: boolean,
337337
): `<:_:${EmojiId}>` | `<a:_:${EmojiId}>`;
338338

339-
export function formatEmoji<EmojiId extends Snowflake>(
340-
emojiId: EmojiId,
341-
animated = false,
342-
): `<:_:${EmojiId}>` | `<a:_:${EmojiId}>` {
343-
return `<${animated ? 'a' : ''}:_:${emojiId}>`;
339+
/**
340+
* Formats a non-animated emoji id and name into a fully qualified emoji identifier.
341+
*
342+
* @typeParam EmojiId - This is inferred by the supplied emoji id
343+
* @typeParam EmojiName - This is inferred by the supplied name
344+
* @param options - The options for formatting an emoji
345+
*/
346+
export function formatEmoji<EmojiId extends Snowflake, EmojiName extends string>(
347+
options: FormatEmojiOptions<EmojiId, EmojiName> & { animated: true },
348+
): `<a:${EmojiName}:${EmojiId}>`;
349+
350+
/**
351+
* Formats an animated emoji id and name into a fully qualified emoji identifier.
352+
*
353+
* @typeParam EmojiId - This is inferred by the supplied emoji id
354+
* @typeParam EmojiName - This is inferred by the supplied name
355+
* @param options - The options for formatting an emoji
356+
*/
357+
export function formatEmoji<EmojiId extends Snowflake, EmojiName extends string>(
358+
options: FormatEmojiOptions<EmojiId, EmojiName> & { animated?: false },
359+
): `<:${EmojiName}:${EmojiId}>`;
360+
361+
/**
362+
* Formats an emoji id and name into a fully qualified emoji identifier.
363+
*
364+
* @typeParam EmojiId - This is inferred by the supplied emoji id
365+
* @typeParam EmojiName - This is inferred by the supplied emoji name
366+
* @param options - The options for formatting an emoji
367+
*/
368+
export function formatEmoji<EmojiId extends Snowflake, EmojiName extends string>(
369+
options: FormatEmojiOptions<EmojiId, EmojiName>,
370+
): `<:${EmojiName}:${EmojiId}>` | `<a:${EmojiName}:${EmojiId}>`;
371+
372+
export function formatEmoji<EmojiId extends Snowflake, EmojiName extends string>(
373+
emojiIdOrOptions: EmojiId | FormatEmojiOptions<EmojiId, EmojiName>,
374+
animated?: boolean,
375+
): `<:${string}:${EmojiId}>` | `<a:${string}:${EmojiId}>` {
376+
const options =
377+
typeof emojiIdOrOptions === 'string'
378+
? {
379+
id: emojiIdOrOptions,
380+
animated: animated ?? false,
381+
}
382+
: emojiIdOrOptions;
383+
384+
const { id, animated: isAnimated, name: emojiName } = options;
385+
386+
return `<${isAnimated ? 'a' : ''}:${emojiName ?? '_'}:${id}>`;
387+
}
388+
389+
/**
390+
* The options for formatting an emoji.
391+
*
392+
* @typeParam EmojiId - This is inferred by the supplied emoji id
393+
* @typeParam EmojiName - This is inferred by the supplied emoji name
394+
*/
395+
export interface FormatEmojiOptions<EmojiId extends Snowflake, EmojiName extends string> {
396+
/**
397+
* Whether the emoji is animated
398+
*/
399+
animated?: boolean;
400+
/**
401+
* The emoji id to format
402+
*/
403+
id: EmojiId;
404+
/**
405+
* The name of the emoji
406+
*/
407+
name?: EmojiName;
344408
}
345409

346410
/**

0 commit comments

Comments
 (0)