mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-20 13:48:09 +00:00
[Glitch] Emoji Indexing and Search
Port a1e8813522 to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
import { readdir } from 'fs/promises';
|
||||
import { basename, resolve } from 'path';
|
||||
|
||||
import unicodeEmojis from 'emojibase-data/en/data.json';
|
||||
import { flattenEmojiData } from 'emojibase';
|
||||
import unicodeRawEmojis from 'emojibase-data/en/data.json';
|
||||
|
||||
import { twemojiToUnicodeInfo, unicodeToTwemojiHex } from './normalize';
|
||||
import {
|
||||
twemojiHasBorder,
|
||||
twemojiToUnicodeInfo,
|
||||
unicodeToTwemojiHex,
|
||||
CODES_WITH_DARK_BORDER,
|
||||
CODES_WITH_LIGHT_BORDER,
|
||||
emojiToUnicodeHex,
|
||||
} from './normalize';
|
||||
|
||||
const emojiSVGFiles = await readdir(
|
||||
// This assumes tests are run from project root
|
||||
@@ -13,60 +21,81 @@ const emojiSVGFiles = await readdir(
|
||||
},
|
||||
);
|
||||
const svgFileNames = emojiSVGFiles
|
||||
.filter(
|
||||
(file) =>
|
||||
file.isFile() &&
|
||||
file.name.endsWith('.svg') &&
|
||||
!file.name.endsWith('_border.svg'),
|
||||
)
|
||||
.filter((file) => file.isFile() && file.name.endsWith('.svg'))
|
||||
.map((file) => basename(file.name, '.svg').toUpperCase());
|
||||
const svgFileNamesWithoutBorder = svgFileNames.filter(
|
||||
(fileName) => !fileName.endsWith('_BORDER'),
|
||||
);
|
||||
|
||||
describe('normalizeEmoji', () => {
|
||||
describe('unicodeToSVGName', () => {
|
||||
test.concurrent.for(
|
||||
unicodeEmojis
|
||||
// Our version of Twemoji only supports up to version 15.1
|
||||
.filter((emoji) => emoji.version < 16)
|
||||
.map((emoji) => [emoji.hexcode, emoji.label] as [string, string]),
|
||||
)('verifying an emoji exists for %s (%s)', ([hexcode], { expect }) => {
|
||||
const result = unicodeToTwemojiHex(hexcode);
|
||||
expect(svgFileNames).toContain(result);
|
||||
});
|
||||
});
|
||||
const unicodeEmojis = flattenEmojiData(unicodeRawEmojis);
|
||||
|
||||
describe('twemojiToUnicodeInfo', () => {
|
||||
const unicodeMap = new Map(
|
||||
unicodeEmojis.flatMap((emoji) => {
|
||||
const base: [string, string][] = [[emoji.hexcode, emoji.label]];
|
||||
if (emoji.skins) {
|
||||
base.push(
|
||||
...emoji.skins.map(
|
||||
(skin) => [skin.hexcode, skin.label] as [string, string],
|
||||
),
|
||||
);
|
||||
}
|
||||
return base;
|
||||
}),
|
||||
);
|
||||
describe('emojiToUnicodeHex', () => {
|
||||
test.concurrent.for([
|
||||
['🎱', '1F3B1'],
|
||||
['🐜', '1F41C'],
|
||||
['⚫', '26AB'],
|
||||
['🖤', '1F5A4'],
|
||||
['💀', '1F480'],
|
||||
['💂♂️', '1F482-200D-2642-FE0F'],
|
||||
] as const)(
|
||||
'emojiToUnicodeHex converts %s to %s',
|
||||
([emoji, hexcode], { expect }) => {
|
||||
expect(emojiToUnicodeHex(emoji)).toBe(hexcode);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test.concurrent.for(svgFileNames)(
|
||||
'verifying SVG file %s maps to Unicode emoji',
|
||||
(svgFileName, { expect }) => {
|
||||
assert(!!svgFileName);
|
||||
const result = twemojiToUnicodeInfo(svgFileName);
|
||||
const hexcode =
|
||||
typeof result === 'string' ? result : result.unqualified;
|
||||
if (!hexcode) {
|
||||
// No hexcode means this is a special case like the Shibuya 109 emoji
|
||||
expect(result).toHaveProperty('label');
|
||||
return;
|
||||
}
|
||||
assert(!!hexcode);
|
||||
expect(
|
||||
unicodeMap.has(hexcode),
|
||||
`${hexcode} (${svgFileName}) not found`,
|
||||
).toBeTruthy();
|
||||
},
|
||||
);
|
||||
describe('unicodeToTwemojiHex', () => {
|
||||
test.concurrent.for(
|
||||
unicodeEmojis
|
||||
// Our version of Twemoji only supports up to version 15.1
|
||||
.filter((emoji) => emoji.version < 16)
|
||||
.map((emoji) => [emoji.hexcode, emoji.label] as [string, string]),
|
||||
)('verifying an emoji exists for %s (%s)', ([hexcode], { expect }) => {
|
||||
const result = unicodeToTwemojiHex(hexcode);
|
||||
expect(svgFileNamesWithoutBorder).toContain(result);
|
||||
});
|
||||
});
|
||||
|
||||
describe('twemojiHasBorder', () => {
|
||||
test.concurrent.for(
|
||||
svgFileNames
|
||||
.filter((file) => file.endsWith('_BORDER'))
|
||||
.map((file) => {
|
||||
const hexCode = file.replace('_BORDER', '');
|
||||
return [
|
||||
hexCode,
|
||||
CODES_WITH_LIGHT_BORDER.includes(hexCode),
|
||||
CODES_WITH_DARK_BORDER.includes(hexCode),
|
||||
] as const;
|
||||
}),
|
||||
)('twemojiHasBorder for %s', ([hexCode, isLight, isDark], { expect }) => {
|
||||
const result = twemojiHasBorder(hexCode);
|
||||
expect(result).toHaveProperty('hexCode', hexCode);
|
||||
expect(result).toHaveProperty('hasLightBorder', isLight);
|
||||
expect(result).toHaveProperty('hasDarkBorder', isDark);
|
||||
});
|
||||
});
|
||||
|
||||
describe('twemojiToUnicodeInfo', () => {
|
||||
const unicodeCodeSet = new Set(unicodeEmojis.map((emoji) => emoji.hexcode));
|
||||
|
||||
test.concurrent.for(svgFileNamesWithoutBorder)(
|
||||
'verifying SVG file %s maps to Unicode emoji',
|
||||
(svgFileName, { expect }) => {
|
||||
assert(!!svgFileName);
|
||||
const result = twemojiToUnicodeInfo(svgFileName);
|
||||
const hexcode = typeof result === 'string' ? result : result.unqualified;
|
||||
if (!hexcode) {
|
||||
// No hexcode means this is a special case like the Shibuya 109 emoji
|
||||
expect(result).toHaveProperty('label');
|
||||
return;
|
||||
}
|
||||
assert(!!hexcode);
|
||||
expect(
|
||||
unicodeCodeSet.has(hexcode),
|
||||
`${hexcode} (${svgFileName}) not found`,
|
||||
).toBeTruthy();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user