Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(formatters): Add support for object and name param in formatEmoji() #10076

Merged
merged 3 commits into from Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>(
This conversation was marked as resolved.
Show resolved Hide resolved
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.
This conversation was marked as resolved.
Show resolved Hide resolved
*
* @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