-
Notifications
You must be signed in to change notification settings - Fork 2
Upgrade Emojibase and Add emoticon variations #21
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
Changes from 7 commits
d468b46
0f817b1
741738f
a0b313b
684a1be
0069db6
074b389
c9a3930
f260197
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| Copyright 2019 The Matrix.org Foundation C.I.C. | ||
| Copyright 2019-2024 The Matrix.org Foundation C.I.C. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
|
|
@@ -16,13 +16,21 @@ limitations under the License. | |
|
|
||
| import EMOJIBASE from "emojibase-data/en/compact.json"; | ||
| import SHORTCODES from "emojibase-data/en/shortcodes/iamcal.json"; | ||
| import { CompactEmoji } from "emojibase"; | ||
| import VERSIONS from "emojibase-data/versions/emoji.json"; | ||
| import { CompactEmoji, generateEmoticonPermutations } from "emojibase"; | ||
|
|
||
| export interface Emoji extends Omit<CompactEmoji, "shortcodes"> { | ||
| // We generate a shortcode based on the label if none exist in the dataset | ||
| shortcodes: string[]; | ||
| } | ||
|
|
||
| const MAX_EMOJI_VERSION_WEB = 15.0; | ||
| const EMOJI_TO_VERSION = new Map<string, number>(); | ||
| for (const [versionString, emojis] of Object.entries(VERSIONS)) { | ||
| const version = parseFloat(versionString); | ||
| emojis.forEach((emoji) => EMOJI_TO_VERSION.set(emoji, version)); | ||
| } | ||
|
|
||
| // The unicode is stored without the variant selector | ||
| const UNICODE_TO_EMOJI = new Map<string, Emoji>(); // not exported as gets for it are handled by getEmojiFromUnicode | ||
| export const EMOTICON_TO_EMOJI = new Map<string, Emoji>(); | ||
|
|
@@ -67,8 +75,15 @@ export const DATA_BY_CATEGORY: Record<string, Emoji[]> = { | |
| flags: [], | ||
| }; | ||
|
|
||
| const MAX_EMOJI_VERSION: number = | ||
| parseFloat(<string>process.env.MAX_EMOJI_VERSION) || MAX_EMOJI_VERSION_WEB; | ||
| console.log(`emojibase MAX_EMOJI_VERSION ${MAX_EMOJI_VERSION}`); | ||
| // Store various mappings from unicode/emoticon/shortcode to the Emoji objects | ||
| export const EMOJI: Emoji[] = EMOJIBASE.map((emojiData) => { | ||
| export const EMOJI: Emoji[] = EMOJIBASE.filter((emojiData) => { | ||
| // filter emojis that are less than or equal to MAX_EMOJI_VERSION | ||
| const version = EMOJI_TO_VERSION.get(emojiData.hexcode); | ||
| return version && version <= MAX_EMOJI_VERSION; | ||
| }).map((emojiData) => { | ||
| // If there's ever a gap in shortcode coverage, we fudge it by | ||
| // filling it in with the emoji's CLDR annotation | ||
| const shortcodeData = SHORTCODES[emojiData.hexcode] ?? [ | ||
|
|
@@ -100,15 +115,14 @@ export const EMOJI: Emoji[] = EMOJIBASE.map((emojiData) => { | |
| // 'emoji'. We therefore strip any variation chars from strings | ||
| // both when building the map and when looking up. | ||
| UNICODE_TO_EMOJI.set(stripVariation(emoji.unicode), emoji); | ||
|
|
||
| if (emoji.emoticon) { | ||
| // Add mapping from emoticon to Emoji object | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
| Array.isArray(emoji.emoticon) | ||
| ? emoji.emoticon.forEach((x) => EMOTICON_TO_EMOJI.set(x, emoji)) | ||
| : EMOTICON_TO_EMOJI.set(emoji.emoticon, emoji); | ||
| const emoticons = [emoji.emoticon] | ||
| //flatten, in case `emoji.emoticon` is an array | ||
| .flat() | ||
| .flatMap((x) => generateEmoticonPermutations(x)); | ||
| emoticons.forEach((x) => EMOTICON_TO_EMOJI.set(x, emoji)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's feel wrong to me to mutate a map/array when doing a map another array.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, true I found that strange also, I wasn't too comfortable making those logic changes before as there wasn't much tests but we have that now :). Will move the logic that isn't related to the map out to a foreach so it's a bit clearer. |
||
| } | ||
|
|
||
| return emoji; | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment is welcomed to explain why we need it