Refactor buildSearch function in emoji_utils.ts

The buildSearch function has been refactored for better coding practices. More explicit typings and declaration were used, and the logic related to array search has been simplified for better readability. The 'eslint-disable' comment line was moved to a more appropriate location after these changes.
This commit is contained in:
taichi221228 2024-04-24 09:55:00 +09:00 committed by Eugen Rochko
parent ac2b7a6cfb
commit 88afeb3654

View File

@ -1,28 +1,25 @@
// This code is largely borrowed from: // This code is largely borrowed from:
// https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/index.js // https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/index.js
/* eslint-disable */ import type { Emoji } from 'emoji-mart/dist-es/utils/data';
import * as data from './emoji_mart_data_light'; import * as data from './emoji_mart_data_light';
// @ts-expect-error type Data = Pick<Emoji, 'short_names' | 'name' | 'keywords' | 'emoticons'>;
const buildSearch = (data) => {
// @ts-expect-error
const search = [];
// @ts-expect-error const buildSearch = (data: Data) => {
let addToSearch = (strings, split) => { const search: string[] = [];
const addToSearch = (strings: Data[keyof Data], split: boolean) => {
if (!strings) { if (!strings) {
return; return;
} }
(Array.isArray(strings) ? strings : [strings]).forEach((string) => { (Array.isArray(strings) ? strings : [strings]).forEach((string) => {
// @ts-expect-error
(split ? string.split(/[-|_|\s]+/) : [string]).forEach((s) => { (split ? string.split(/[-|_|\s]+/) : [string]).forEach((s) => {
s = s.toLowerCase(); s = s.toLowerCase();
// @ts-expect-error if (!search.includes(s)) {
if (search.indexOf(s) === -1) {
search.push(s); search.push(s);
} }
}); });
@ -34,10 +31,11 @@ const buildSearch = (data) => {
addToSearch(data.keywords, false); addToSearch(data.keywords, false);
addToSearch(data.emoticons, false); addToSearch(data.emoticons, false);
// @ts-expect-error
return search.join(','); return search.join(',');
}; };
/* eslint-disable */
const _String = String; const _String = String;
const stringFromCodePoint = const stringFromCodePoint =