Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { unicodeToShortname } from './emoji';

describe('unicodeToShortname', () => {
it('converts emoji without variation selector to shortname', () => {
expect(unicodeToShortname('\u{1F44D}')).toBe(':+1:');
});

it('converts emoji with variation selector to shortname', () => {
expect(unicodeToShortname('\u{1F44D}\u{FE0F}')).toBe(':+1:');
});

it('converts skin tone emoji to shortname', () => {
expect(unicodeToShortname('\u{1F44D}\u{1F3FD}')).toBe(':+1_tone3:');
});

it('returns the input unchanged when no shortname exists', () => {
expect(unicodeToShortname('not an emoji')).toBe('not an emoji');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ const firstShortcode = (hexcode: string): string | undefined => {
return Array.isArray(entry) ? entry[0] : entry;
};

const stripVariationSelectors = (unicode: string): string => unicode.replace(/\p{Variation_Selector}/gu, '');

const buildMap = (): Map<string, string> => {
const map = new Map<string, string>();
for (const emoji of data as EmojibaseEntry[]) {
const code = firstShortcode(emoji.hexcode);
if (code) {
map.set(emoji.emoji, `:${code}:`);
map.set(stripVariationSelectors(emoji.emoji), `:${code}:`);
}
for (const skin of emoji.skins ?? []) {
const skinCode = firstShortcode(skin.hexcode);
if (skinCode) {
map.set(skin.emoji, `:${skinCode}:`);
map.set(stripVariationSelectors(skin.emoji), `:${skinCode}:`);
}
}
}
Expand All @@ -34,5 +36,5 @@ export const unicodeToShortname = (unicode: string): string => {
if (!unicodeToShort) {
unicodeToShort = buildMap();
}
return unicodeToShort.get(unicode) ?? unicode;
return unicodeToShort.get(stripVariationSelectors(unicode)) ?? unicode;
};
Loading