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

Commit

Permalink
feat(Formatters): add formatEmoji (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Aug 10, 2021
1 parent 5041c62 commit c3d8bb5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
41 changes: 33 additions & 8 deletions src/messages/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function codeBlock(language: string, content?: string): string {
}

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

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

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

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

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

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

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

/**
* Formats the role ID into a role mention.
* Formats a role ID into a role mention.
* @param roleId The role ID to format.
* @returns The formatted role mention.
*/
export function roleMention<C extends Snowflake>(roleId: C): `<@&${C}>` {
return `<@&${roleId}>`;
}

/**
* Formats an emoji ID into a fully qualified emoji identifier
* @param emojiId The emoji ID to format.
* @returns The formatted emoji.
*/
export function formatEmoji<C extends Snowflake>(emojiId: C, animated?: false): `<:_:${C}>`;

/**
* Formats an emoji ID into a fully qualified emoji identifier
* @param emojiId The emoji ID to format.
* @param animated Whether the emoji is animated or not. Defaults to `false`
* @returns The formatted emoji.
*/
export function formatEmoji<C extends Snowflake>(emojiId: C, animated?: true): `<a:_:${C}>`;

/**
* Formats an emoji ID into a fully qualified emoji identifier
* @param emojiId The emoji ID to format.
* @param animated Whether the emoji is animated or not. Defaults to `false`
* @returns The formatted emoji.
*/
export function formatEmoji<C extends Snowflake>(emojiId: C, animated = false): `<a:_:${C}>` | `<:_:${C}>` {
return `<${animated ? 'a' : ''}:_:${emojiId}>`;
}

/**
* Formats a date into a short date-time string.
* @param date The date to format, defaults to the current time.
Expand Down
19 changes: 17 additions & 2 deletions tests/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
channelMention,
codeBlock,
Faces,
formatEmoji,
hideLinkEmbed,
hyperlink,
spoiler,
inlineCode,
italic,
memberNicknameMention,
quote,
roleMention,
spoiler,
strikethrough,
time,
TimestampStyles,
Expand Down Expand Up @@ -107,7 +108,7 @@ describe('Messages', () => {
).toBe('[discord.js](https://discord.js.org/ "Official Documentation")');
});
});

describe('spoiler', () => {
test('GIVEN "discord.js" THEN returns "||discord.js||"', () => {
expect<'||discord.js||'>(spoiler('discord.js')).toBe('||discord.js||');
Expand Down Expand Up @@ -140,6 +141,20 @@ describe('Messages', () => {
});
});

describe('formatEmoji', () => {
test('GIVEN static emojiId THEN returns "<:_:${emojiId}>"', () => {
expect<`<:_:851461487498493952>`>(formatEmoji('851461487498493952')).toBe('<:_:851461487498493952>');
});

test('GIVEN static emojiId WITH animated explicitly false THEN returns "<:_:[emojiId]>"', () => {
expect<`<:_:851461487498493952>`>(formatEmoji('851461487498493952', false)).toBe('<:_:851461487498493952>');
});

test('GIVEN animated emojiId THEN returns "<a:_:${emojiId}>"', () => {
expect<`<a:_:827220205352255549>`>(formatEmoji('827220205352255549', true)).toBe('<a:_:827220205352255549>');
});
});

describe('time', () => {
test('GIVEN no arguments THEN returns "<t:${bigint}>"', () => {
jest.useFakeTimers('modern');
Expand Down

0 comments on commit c3d8bb5

Please sign in to comment.