1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-28 05:15:54 +00:00

Do less string checking in sortEmotes

This commit is contained in:
cfinegan 2022-01-17 15:57:44 -05:00
parent 0610175906
commit f85f0c49b6

View file

@ -16,6 +16,13 @@ import Twilight from 'site';
const locale = Intl.Collator(); const locale = Intl.Collator();
const localeCaseInsensitive = Intl.Collator(undefined, {sensitivity: 'accent'}); const localeCaseInsensitive = Intl.Collator(undefined, {sensitivity: 'accent'});
// Describes how an emote matches against a given input
// Higher values represent a more exact match
const NO_MATCH = 0;
const NON_PREFIX_MATCH = 1;
const CASE_INSENSITIVE_PREFIX_MATCH = 2;
const EXACT_PREFIX_MATCH = 3;
function getNodeText(node) { function getNodeText(node) {
if ( ! node ) if ( ! node )
return ''; return '';
@ -576,7 +583,10 @@ export default class Input extends Module {
inst.doesEmoteMatchTerm = function(emote, term) { inst.doesEmoteMatchTerm = function(emote, term) {
const emote_name = emote.name || emote.token; const emote_name = emote.name || emote.token;
if ( ! emote_name ) if ( ! emote_name )
return false; return NO_MATCH;
if (emote_name.startsWith(term))
return EXACT_PREFIX_MATCH;
let emote_lower = emote.tokenLower; let emote_lower = emote.tokenLower;
if ( ! emote_lower ) if ( ! emote_lower )
@ -584,13 +594,13 @@ export default class Input extends Module {
const term_lower = term.toLowerCase(); const term_lower = term.toLowerCase();
if (emote_lower.startsWith(term_lower)) if (emote_lower.startsWith(term_lower))
return true; return CASE_INSENSITIVE_PREFIX_MATCH;
const idx = emote_name.indexOf(term.charAt(0).toUpperCase()); const idx = emote_name.indexOf(term.charAt(0).toUpperCase());
if (idx !== -1) if (idx !== -1 && emote_lower.slice(idx + 1).startsWith(term_lower.slice(1)))
return emote_lower.slice(idx + 1).startsWith(term_lower.slice(1)); return NON_PREFIX_MATCH;
return false; return NO_MATCH;
} }
inst.getMatchedEmotes = function(input) { inst.getMatchedEmotes = function(input) {
@ -653,17 +663,6 @@ export default class Input extends Module {
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
sortEmotes(emotes, input) { sortEmotes(emotes, input) {
const preferFavorites = this.chat.context.get('chat.tab-complete.prioritize-favorites'); const preferFavorites = this.chat.context.get('chat.tab-complete.prioritize-favorites');
const lowerCaseInput = input.toLowerCase();
const startsWithInput = new Set();
const startsWithInputCI = new Set(); // case insensitive
for (let i = 0; i < emotes.length; ++i) {
const str = emotes[i].replacement;
if (str.startsWith(input))
startsWithInput.add(str);
else if (str.toLowerCase().startsWith(lowerCaseInput))
startsWithInputCI.add(str);
}
return emotes.sort((a, b) => { return emotes.sort((a, b) => {
const aStr = a.replacement; const aStr = a.replacement;
@ -674,16 +673,16 @@ export default class Input extends Module {
return 0 - a.favorite + b.favorite; return 0 - a.favorite + b.favorite;
// Prefer case-sensitive prefix matches // Prefer case-sensitive prefix matches
const aStartsWithInput = startsWithInput.has(aStr); const aStartsWithInput = (a.match_type === EXACT_PREFIX_MATCH);
const bStartsWithInput = startsWithInput.has(bStr); const bStartsWithInput = (b.match_type === EXACT_PREFIX_MATCH);
if (aStartsWithInput && bStartsWithInput) if (aStartsWithInput && bStartsWithInput)
return locale.compare(aStr, bStr); return locale.compare(aStr, bStr);
else if (aStartsWithInput) return -1; else if (aStartsWithInput) return -1;
else if (bStartsWithInput) return 1; else if (bStartsWithInput) return 1;
// Else prefer case-insensitive prefix matches // Else prefer case-insensitive prefix matches
const aStartsWithInputCI = aStartsWithInput || startsWithInputCI.has(aStr); const aStartsWithInputCI = (a.match_type === CASE_INSENSITIVE_PREFIX_MATCH);
const bStartsWithInputCI = bStartsWithInput || startsWithInputCI.has(bStr); const bStartsWithInputCI = (b.match_type === CASE_INSENSITIVE_PREFIX_MATCH);
if (aStartsWithInputCI && bStartsWithInputCI) if (aStartsWithInputCI && bStartsWithInputCI)
return localeCaseInsensitive.compare(aStr, bStr); return localeCaseInsensitive.compare(aStr, bStr);
else if (aStartsWithInputCI) return -1; else if (aStartsWithInputCI) return -1;
@ -785,19 +784,21 @@ export default class Input extends Module {
search = input.startsWith(':') ? input.slice(1) : input; search = input.startsWith(':') ? input.slice(1) : input;
for(const emote of emotes) { for(const emote of emotes) {
if ( inst.doesEmoteMatchTerm(emote, search) ) { const match_type = inst.doesEmoteMatchTerm(emote, search);
if ( match_type !== NO_MATCH ) {
const element = { const element = {
current: input, current: input,
emote, emote,
replacement: emote.token, replacement: emote.token,
element: inst.renderEmoteSuggestion(emote), element: inst.renderEmoteSuggestion(emote),
favorite: emote.favorite, favorite: emote.favorite,
count: this.EmoteUsageCount[emote.token] || 0 count: this.EmoteUsageCount[emote.token] || 0,
match_type
}; };
if ( element.count > 0 ) if ( element.count > 0 )
results_usage.push(element); results_usage.push(element);
else if ( emote.token.toLowerCase().startsWith(search) ) else if ( match_type > NON_PREFIX_MATCH )
results_starting.push(element); results_starting.push(element);
else else
results_other.push(element); results_other.push(element);
@ -943,14 +944,16 @@ export default class Input extends Module {
results = []; results = [];
for(const emote of emotes) { for(const emote of emotes) {
if ( inst.doesEmoteMatchTerm(emote, search) ) const match_type = inst.doesEmoteMatchTerm(emote, search)
if ( match_type !== NO_MATCH )
results.push({ results.push({
current: input, current: input,
emote, emote,
replacement: emote.token, replacement: emote.token,
element: inst.renderEmoteSuggestion(emote), element: inst.renderEmoteSuggestion(emote),
favorite: emote.favorite, favorite: emote.favorite,
count: 0 // TODO: Count stuff? count: 0, // TODO: Count stuff?
match_type
}); });
} }