Skip to content
This repository was archived by the owner on Jan 8, 2022. It is now read-only.

Commit c3d8bb5

Browse files
authored
feat(Formatters): add formatEmoji (#20)
1 parent 5041c62 commit c3d8bb5

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/messages/formatters.ts

+33-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function codeBlock(language: string, content?: string): string {
2020
}
2121

2222
/**
23-
* Wraps the content inside an inline code.
23+
* Wraps the content inside \`backticks\`, which formats it as inline code.
2424
* @param content The content to wrap.
2525
* @returns The formatted content.
2626
*/
@@ -56,7 +56,7 @@ export function underscore<C extends string>(content: C): `__${C}__` {
5656
}
5757

5858
/**
59-
* Formats the content into strikethrough text.
59+
* Formats the content into strike-through text.
6060
* @param content The content to wrap.
6161
* @returns The formatted content.
6262
*/
@@ -83,14 +83,14 @@ export function blockQuote<C extends string>(content: C): `>>> ${C}` {
8383
}
8484

8585
/**
86-
* Formats the URL into `<>`, which stops it from embedding.
86+
* Wraps the URL into `<>`, which stops it from embedding.
8787
* @param url The URL to wrap.
8888
* @returns The formatted content.
8989
*/
9090
export function hideLinkEmbed<C extends string>(url: C): `<${C}>`;
9191

9292
/**
93-
* Formats the URL into `<>`, which stops it from embedding.
93+
* Wraps the URL into `<>`, which stops it from embedding.
9494
* @param url The URL to wrap.
9595
* @returns The formatted content.
9696
*/
@@ -156,7 +156,7 @@ export function spoiler<C extends string>(content: C): `||${C}||` {
156156
}
157157

158158
/**
159-
* Formats the user ID into a user mention.
159+
* Formats a user ID into a user mention.
160160
* @param userId The user ID to format.
161161
* @returns The formatted user mention.
162162
*/
@@ -165,7 +165,7 @@ export function userMention<C extends Snowflake>(userId: C): `<@${C}>` {
165165
}
166166

167167
/**
168-
* Formats the user ID into a member-nickname mention.
168+
* Formats a user ID into a member-nickname mention.
169169
* @param memberId The user ID to format.
170170
* @returns The formatted member-nickname mention.
171171
*/
@@ -174,7 +174,7 @@ export function memberNicknameMention<C extends Snowflake>(memberId: C): `<@!${C
174174
}
175175

176176
/**
177-
* Formats the channel ID into a channel mention.
177+
* Formats a channel ID into a channel mention.
178178
* @param channelId The channel ID to format.
179179
* @returns The formatted channel mention.
180180
*/
@@ -183,14 +183,39 @@ export function channelMention<C extends Snowflake>(channelId: C): `<#${C}>` {
183183
}
184184

185185
/**
186-
* Formats the role ID into a role mention.
186+
* Formats a role ID into a role mention.
187187
* @param roleId The role ID to format.
188188
* @returns The formatted role mention.
189189
*/
190190
export function roleMention<C extends Snowflake>(roleId: C): `<@&${C}>` {
191191
return `<@&${roleId}>`;
192192
}
193193

194+
/**
195+
* Formats an emoji ID into a fully qualified emoji identifier
196+
* @param emojiId The emoji ID to format.
197+
* @returns The formatted emoji.
198+
*/
199+
export function formatEmoji<C extends Snowflake>(emojiId: C, animated?: false): `<:_:${C}>`;
200+
201+
/**
202+
* Formats an emoji ID into a fully qualified emoji identifier
203+
* @param emojiId The emoji ID to format.
204+
* @param animated Whether the emoji is animated or not. Defaults to `false`
205+
* @returns The formatted emoji.
206+
*/
207+
export function formatEmoji<C extends Snowflake>(emojiId: C, animated?: true): `<a:_:${C}>`;
208+
209+
/**
210+
* Formats an emoji ID into a fully qualified emoji identifier
211+
* @param emojiId The emoji ID to format.
212+
* @param animated Whether the emoji is animated or not. Defaults to `false`
213+
* @returns The formatted emoji.
214+
*/
215+
export function formatEmoji<C extends Snowflake>(emojiId: C, animated = false): `<a:_:${C}>` | `<:_:${C}>` {
216+
return `<${animated ? 'a' : ''}:_:${emojiId}>`;
217+
}
218+
194219
/**
195220
* Formats a date into a short date-time string.
196221
* @param date The date to format, defaults to the current time.

tests/messages.test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import {
44
channelMention,
55
codeBlock,
66
Faces,
7+
formatEmoji,
78
hideLinkEmbed,
89
hyperlink,
9-
spoiler,
1010
inlineCode,
1111
italic,
1212
memberNicknameMention,
1313
quote,
1414
roleMention,
15+
spoiler,
1516
strikethrough,
1617
time,
1718
TimestampStyles,
@@ -107,7 +108,7 @@ describe('Messages', () => {
107108
).toBe('[discord.js](https://discord.js.org/ "Official Documentation")');
108109
});
109110
});
110-
111+
111112
describe('spoiler', () => {
112113
test('GIVEN "discord.js" THEN returns "||discord.js||"', () => {
113114
expect<'||discord.js||'>(spoiler('discord.js')).toBe('||discord.js||');
@@ -140,6 +141,20 @@ describe('Messages', () => {
140141
});
141142
});
142143

144+
describe('formatEmoji', () => {
145+
test('GIVEN static emojiId THEN returns "<:_:${emojiId}>"', () => {
146+
expect<`<:_:851461487498493952>`>(formatEmoji('851461487498493952')).toBe('<:_:851461487498493952>');
147+
});
148+
149+
test('GIVEN static emojiId WITH animated explicitly false THEN returns "<:_:[emojiId]>"', () => {
150+
expect<`<:_:851461487498493952>`>(formatEmoji('851461487498493952', false)).toBe('<:_:851461487498493952>');
151+
});
152+
153+
test('GIVEN animated emojiId THEN returns "<a:_:${emojiId}>"', () => {
154+
expect<`<a:_:827220205352255549>`>(formatEmoji('827220205352255549', true)).toBe('<a:_:827220205352255549>');
155+
});
156+
});
157+
143158
describe('time', () => {
144159
test('GIVEN no arguments THEN returns "<t:${bigint}>"', () => {
145160
jest.useFakeTimers('modern');

0 commit comments

Comments
 (0)