Skip to content

Commit

Permalink
refactor(formatters): Add support for object and name param in `forma…
Browse files Browse the repository at this point in the history
…tEmoji()` (#10076)

* feat: add support for name param and object in `formatEmoji()`

* Update formatters.ts

* refactor: swap priority

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
  • Loading branch information
codershiba and Jiralite authored Jan 13, 2024
1 parent 136c66c commit 7b8e0de
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/Emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
34 changes: 34 additions & 0 deletions packages/formatters/__tests__/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,40 @@ describe('Message formatters', () => {
test('GIVEN animated emojiId THEN returns "<a:_:${emojiId}>"', () => {
expect<`<a:_:827220205352255549>`>(formatEmoji('827220205352255549', true)).toEqual('<a:_:827220205352255549>');
});

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 "<a:_:${id}>"', () => {
expect<`<a:_:827220205352255549>`>(formatEmoji({ animated: true, id: '827220205352255549' })).toEqual(
'<a:_:827220205352255549>',
);
});

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 "<a:${name}:${id}>"', () => {
expect<`<a:test:827220205352255549>`>(
formatEmoji({ id: '827220205352255549', name: 'test', animated: true }),
).toEqual('<a:test:827220205352255549>');
});
});

describe('channelLink', () => {
Expand Down
74 changes: 69 additions & 5 deletions packages/formatters/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,75 @@ export function formatEmoji<EmojiId extends Snowflake>(
animated?: boolean,
): `<:_:${EmojiId}>` | `<a:_:${EmojiId}>`;

export function formatEmoji<EmojiId extends Snowflake>(
emojiId: EmojiId,
animated = false,
): `<:_:${EmojiId}>` | `<a:_:${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<EmojiId extends Snowflake, EmojiName extends string>(
options: FormatEmojiOptions<EmojiId, EmojiName> & { animated: true },
): `<a:${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<EmojiId extends Snowflake, EmojiName extends string>(
options: FormatEmojiOptions<EmojiId, EmojiName> & { animated?: false },
): `<:${EmojiName}:${EmojiId}>`;

/**
* 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<EmojiId extends Snowflake, EmojiName extends string>(
options: FormatEmojiOptions<EmojiId, EmojiName>,
): `<:${EmojiName}:${EmojiId}>` | `<a:${EmojiName}:${EmojiId}>`;

export function formatEmoji<EmojiId extends Snowflake, EmojiName extends string>(
emojiIdOrOptions: EmojiId | FormatEmojiOptions<EmojiId, EmojiName>,
animated?: boolean,
): `<:${string}:${EmojiId}>` | `<a:${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<EmojiId extends Snowflake, EmojiName extends string> {
/**
* Whether the emoji is animated
*/
animated?: boolean;
/**
* The emoji id to format
*/
id: EmojiId;
/**
* The name of the emoji
*/
name?: EmojiName;
}

/**
Expand Down

0 comments on commit 7b8e0de

Please sign in to comment.