From 46d4c17b754bb91a64182cb36d30ed8c8ac3b95b Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Mon, 18 Sep 2023 18:00:18 +0200 Subject: [PATCH 1/4] [TS migration] migrate 'EmojiTrie.js' lib --- src/libs/{EmojiTrie.js => EmojiTrie.ts} | 58 ++++++++++++++++--------- src/libs/Trie/TrieNode.ts | 11 ++++- 2 files changed, 48 insertions(+), 21 deletions(-) rename src/libs/{EmojiTrie.js => EmojiTrie.ts} (57%) diff --git a/src/libs/EmojiTrie.js b/src/libs/EmojiTrie.ts similarity index 57% rename from src/libs/EmojiTrie.js rename to src/libs/EmojiTrie.ts index 00e5fc1388e1..ecd4dcee0635 100644 --- a/src/libs/EmojiTrie.js +++ b/src/libs/EmojiTrie.ts @@ -1,8 +1,25 @@ -import _ from 'underscore'; +import React from 'react'; +import {SvgProps} from 'react-native-svg'; import emojis, {localeEmojis} from '../../assets/emojis'; import Trie from './Trie'; import Timing from './actions/Timing'; import CONST from '../CONST'; +import {MetaData} from './Trie/TrieNode'; + +type Emoji = { + code: string; + header?: boolean; + icon?: React.FC; + name?: string; + types?: string[]; +}; + +type LangEmoji = { + name?: string; + keywords: string[]; +}; + +type LangEmojis = Record; Timing.start(CONST.TIMING.TRIE_INITIALIZATION); @@ -10,20 +27,20 @@ const supportedLanguages = [CONST.LOCALES.DEFAULT, CONST.LOCALES.ES]; /** * - * @param {Trie} trie The Trie object. - * @param {Array} keywords An array containing the keywords. - * @param {Object} item An object containing the properties of the emoji. - * @param {String} name The localized name of the emoji. - * @param {Boolean} shouldPrependKeyword Prepend the keyword (instead of append) to the suggestions + * @param trie The Trie object. + * @param keywords An array containing the keywords. + * @param item An object containing the properties of the emoji. + * @param name The localized name of the emoji. + * @param shouldPrependKeyword Prepend the keyword (instead of append) to the suggestions */ -function addKeywordsToTrie(trie, keywords, item, name, shouldPrependKeyword = false) { - _.forEach(keywords, (keyword) => { +function addKeywordsToTrie(trie: Trie, keywords: string[], item: Emoji, name: string, shouldPrependKeyword = false): void { + keywords.forEach((keyword) => { const keywordNode = trie.search(keyword); if (!keywordNode) { trie.add(keyword, {suggestions: [{code: item.code, types: item.types, name}]}); } else { const suggestion = {code: item.code, types: item.types, name}; - const suggestions = shouldPrependKeyword ? [suggestion, ...keywordNode.metaData.suggestions] : [...keywordNode.metaData.suggestions, suggestion]; + const suggestions = shouldPrependKeyword ? [suggestion, ...(keywordNode.metaData.suggestions ?? [])] : [...(keywordNode.metaData.suggestions ?? []), suggestion]; trie.update(keyword, { ...keywordNode.metaData, suggestions, @@ -35,26 +52,27 @@ function addKeywordsToTrie(trie, keywords, item, name, shouldPrependKeyword = fa /** * Allows searching based on parts of the name. This turns 'white_large_square' into ['white_large_square', 'large_square', 'square']. * - * @param {String} name The emoji name - * @returns {Array} An array containing the name parts + * @param name The emoji name + * @returns An array containing the name parts */ -function getNameParts(name) { +function getNameParts(name: string): string[] { const nameSplit = name.split('_'); - return _.map(nameSplit, (_namePart, index) => nameSplit.slice(index).join('_')); + return nameSplit.map((namePart, index) => nameSplit.slice(index).join('_')); } -function createTrie(lang = CONST.LOCALES.DEFAULT) { +function createTrie(lang: 'en' | 'es' = CONST.LOCALES.DEFAULT): Trie { const trie = new Trie(); - const langEmojis = localeEmojis[lang]; + const langEmojis: LangEmojis = localeEmojis[lang]; + const defaultLangEmojis: LangEmojis = localeEmojis[CONST.LOCALES.DEFAULT]; const isDefaultLocale = lang === CONST.LOCALES.DEFAULT; - _.forEach(emojis, (item) => { - if (item.header) { + emojis.forEach((item: Emoji) => { + if (!item.name) { return; } const englishName = item.name; - const localeName = _.get(langEmojis, [item.code, 'name'], englishName); + const localeName = langEmojis?.[item.code]?.name ?? englishName; const node = trie.search(localeName); if (!node) { @@ -67,7 +85,7 @@ function createTrie(lang = CONST.LOCALES.DEFAULT) { addKeywordsToTrie(trie, nameParts, item, localeName); // Add keywords for both the locale language and English to enable users to search using either language. - const keywords = _.get(langEmojis, [item.code, 'keywords'], []).concat(isDefaultLocale ? [] : _.get(localeEmojis, [CONST.LOCALES.DEFAULT, item.code, 'keywords'], [])); + const keywords = (langEmojis?.[item.code]?.keywords ?? []).concat(isDefaultLocale ? [] : defaultLangEmojis?.[item.code]?.keywords ?? []); addKeywordsToTrie(trie, keywords, item, localeName); /** @@ -83,7 +101,7 @@ function createTrie(lang = CONST.LOCALES.DEFAULT) { return trie; } -const emojiTrie = _.reduce(supportedLanguages, (prev, cur) => ({...prev, [cur]: createTrie(cur)}), {}); +const emojiTrie = supportedLanguages.reduce((prev, cur) => ({...prev, [cur]: createTrie(cur)}), {}); Timing.end(CONST.TIMING.TRIE_INITIALIZATION); diff --git a/src/libs/Trie/TrieNode.ts b/src/libs/Trie/TrieNode.ts index 645ce6747cbb..bd989ad6b231 100644 --- a/src/libs/Trie/TrieNode.ts +++ b/src/libs/Trie/TrieNode.ts @@ -1,4 +1,13 @@ -type MetaData = Record; +type Suggestion = { + code: string; + types?: string[]; + name?: string; +}; + +type MetaData = { + [key: string]: unknown; + suggestions?: Suggestion[]; +}; class TrieNode { children: Record>; From 9923cedacbec3b80188751b74da24d05b0de246a Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Mon, 18 Sep 2023 19:26:24 +0200 Subject: [PATCH 2/4] Add SupportedLanguage and EmojiTrie types --- src/libs/EmojiTrie.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libs/EmojiTrie.ts b/src/libs/EmojiTrie.ts index ecd4dcee0635..5cb02826ee5c 100644 --- a/src/libs/EmojiTrie.ts +++ b/src/libs/EmojiTrie.ts @@ -23,7 +23,13 @@ type LangEmojis = Record; Timing.start(CONST.TIMING.TRIE_INITIALIZATION); -const supportedLanguages = [CONST.LOCALES.DEFAULT, CONST.LOCALES.ES]; +const supportedLanguages = [CONST.LOCALES.DEFAULT, CONST.LOCALES.ES] as const; + +type SupportedLanguage = (typeof supportedLanguages)[number]; + +type EmojiTrie = { + [key in SupportedLanguage]?: Trie; +}; /** * @@ -33,7 +39,7 @@ const supportedLanguages = [CONST.LOCALES.DEFAULT, CONST.LOCALES.ES]; * @param name The localized name of the emoji. * @param shouldPrependKeyword Prepend the keyword (instead of append) to the suggestions */ -function addKeywordsToTrie(trie: Trie, keywords: string[], item: Emoji, name: string, shouldPrependKeyword = false): void { +function addKeywordsToTrie(trie: Trie, keywords: string[], item: Emoji, name: string, shouldPrependKeyword = false) { keywords.forEach((keyword) => { const keywordNode = trie.search(keyword); if (!keywordNode) { @@ -60,7 +66,7 @@ function getNameParts(name: string): string[] { return nameSplit.map((namePart, index) => nameSplit.slice(index).join('_')); } -function createTrie(lang: 'en' | 'es' = CONST.LOCALES.DEFAULT): Trie { +function createTrie(lang: SupportedLanguage = CONST.LOCALES.DEFAULT): Trie { const trie = new Trie(); const langEmojis: LangEmojis = localeEmojis[lang]; const defaultLangEmojis: LangEmojis = localeEmojis[CONST.LOCALES.DEFAULT]; @@ -101,7 +107,7 @@ function createTrie(lang: 'en' | 'es' = CONST.LOCALES.DEFAULT): Trie { return trie; } -const emojiTrie = supportedLanguages.reduce((prev, cur) => ({...prev, [cur]: createTrie(cur)}), {}); +const emojiTrie: EmojiTrie = supportedLanguages.reduce((prev, cur) => ({...prev, [cur]: createTrie(cur)}), {}); Timing.end(CONST.TIMING.TRIE_INITIALIZATION); From b41a29678e1af8a238c5097521a94fafdbf329f9 Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Tue, 19 Sep 2023 12:28:40 +0200 Subject: [PATCH 3/4] Update type name --- src/libs/EmojiTrie.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/EmojiTrie.ts b/src/libs/EmojiTrie.ts index 5cb02826ee5c..d972f9897356 100644 --- a/src/libs/EmojiTrie.ts +++ b/src/libs/EmojiTrie.ts @@ -14,12 +14,12 @@ type Emoji = { types?: string[]; }; -type LangEmoji = { +type LocalizedEmoji = { name?: string; keywords: string[]; }; -type LangEmojis = Record; +type LocalizedEmojis = Record; Timing.start(CONST.TIMING.TRIE_INITIALIZATION); @@ -68,8 +68,8 @@ function getNameParts(name: string): string[] { function createTrie(lang: SupportedLanguage = CONST.LOCALES.DEFAULT): Trie { const trie = new Trie(); - const langEmojis: LangEmojis = localeEmojis[lang]; - const defaultLangEmojis: LangEmojis = localeEmojis[CONST.LOCALES.DEFAULT]; + const langEmojis: LocalizedEmojis = localeEmojis[lang]; + const defaultLangEmojis: LocalizedEmojis = localeEmojis[CONST.LOCALES.DEFAULT]; const isDefaultLocale = lang === CONST.LOCALES.DEFAULT; emojis.forEach((item: Emoji) => { From a02e1859d4ec19a096e4e869723ca5f71f0fdbff Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Tue, 19 Sep 2023 15:40:42 +0200 Subject: [PATCH 4/4] Add EmojiMetaData type --- src/libs/EmojiTrie.ts | 17 +++++++++++++---- src/libs/Trie/TrieNode.ts | 11 +---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libs/EmojiTrie.ts b/src/libs/EmojiTrie.ts index d972f9897356..d0a53acf29c9 100644 --- a/src/libs/EmojiTrie.ts +++ b/src/libs/EmojiTrie.ts @@ -4,7 +4,6 @@ import emojis, {localeEmojis} from '../../assets/emojis'; import Trie from './Trie'; import Timing from './actions/Timing'; import CONST from '../CONST'; -import {MetaData} from './Trie/TrieNode'; type Emoji = { code: string; @@ -21,6 +20,16 @@ type LocalizedEmoji = { type LocalizedEmojis = Record; +type Suggestion = { + code: string; + types?: string[]; + name?: string; +}; + +type EmojiMetaData = { + suggestions?: Suggestion[]; +}; + Timing.start(CONST.TIMING.TRIE_INITIALIZATION); const supportedLanguages = [CONST.LOCALES.DEFAULT, CONST.LOCALES.ES] as const; @@ -28,7 +37,7 @@ const supportedLanguages = [CONST.LOCALES.DEFAULT, CONST.LOCALES.ES] as const; type SupportedLanguage = (typeof supportedLanguages)[number]; type EmojiTrie = { - [key in SupportedLanguage]?: Trie; + [key in SupportedLanguage]?: Trie; }; /** @@ -39,7 +48,7 @@ type EmojiTrie = { * @param name The localized name of the emoji. * @param shouldPrependKeyword Prepend the keyword (instead of append) to the suggestions */ -function addKeywordsToTrie(trie: Trie, keywords: string[], item: Emoji, name: string, shouldPrependKeyword = false) { +function addKeywordsToTrie(trie: Trie, keywords: string[], item: Emoji, name: string, shouldPrependKeyword = false) { keywords.forEach((keyword) => { const keywordNode = trie.search(keyword); if (!keywordNode) { @@ -66,7 +75,7 @@ function getNameParts(name: string): string[] { return nameSplit.map((namePart, index) => nameSplit.slice(index).join('_')); } -function createTrie(lang: SupportedLanguage = CONST.LOCALES.DEFAULT): Trie { +function createTrie(lang: SupportedLanguage = CONST.LOCALES.DEFAULT): Trie { const trie = new Trie(); const langEmojis: LocalizedEmojis = localeEmojis[lang]; const defaultLangEmojis: LocalizedEmojis = localeEmojis[CONST.LOCALES.DEFAULT]; diff --git a/src/libs/Trie/TrieNode.ts b/src/libs/Trie/TrieNode.ts index bd989ad6b231..645ce6747cbb 100644 --- a/src/libs/Trie/TrieNode.ts +++ b/src/libs/Trie/TrieNode.ts @@ -1,13 +1,4 @@ -type Suggestion = { - code: string; - types?: string[]; - name?: string; -}; - -type MetaData = { - [key: string]: unknown; - suggestions?: Suggestion[]; -}; +type MetaData = Record; class TrieNode { children: Record>;