1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-09-16 18:06:55 +00:00

- Prioritize emotes that match the search term at the start of a capitalized section, similar to the old NON_PREFIX_MATCH behavior. Now called CAPITALIZED_SECTION_MATCH.

- Search term matches that don't meet any priority criteria are now called SUBSTRING_MATCH.
- Simplified the sortEmotes logic when prioritizing emotes that start with the search term.
This commit is contained in:
OMGparticles 2023-04-07 17:24:52 -07:00
parent e8fdacea5b
commit 0cc2dd4407

View file

@ -18,10 +18,15 @@ const localeCaseInsensitive = Intl.Collator(undefined, {sensitivity: 'accent'});
// Describes how an emote matches against a given input // Describes how an emote matches against a given input
// Higher values represent a more exact match // Higher values represent a more exact match
const NO_MATCH = 0; const NO_MATCH = 0; // Search term doesn't exist in the emote's name
const NON_PREFIX_MATCH = 1; const SUBSTRING_MATCH = 1; // Term exists somewhere in the emote's name
const CASE_INSENSITIVE_PREFIX_MATCH = 2; const CAPITALIZED_SECTION_MATCH = 2; // Term exists at the start of a capitalized section in the emote
const EXACT_PREFIX_MATCH = 3; const CASE_INSENSITIVE_PREFIX_MATCH = 3; // Term characters exist at the start of the emote's name
const EXACT_PREFIX_MATCH = 4; // Term characters and casing exactly match the start of the emote
function isUpperChar(char) {
return (char.length === 1) && (char !== char.toLocaleLowerCase());
}
function getNodeText(node) { function getNodeText(node) {
if ( ! node ) if ( ! node )
@ -820,13 +825,17 @@ export default class Input extends Module {
let emote_lower = emote.tokenLower; let emote_lower = emote.tokenLower;
if ( ! emote_lower ) if ( ! emote_lower )
emote_lower = emote_name.toLowerCase(); emote_lower = emote_name.toLowerCase();
const term_lower = term.toLowerCase(); const term_lower = term.toLowerCase();
if (emote_lower.startsWith(term_lower)) const termPosition = emote_lower.search(term_lower);
if (termPosition == 0)
return CASE_INSENSITIVE_PREFIX_MATCH; return CASE_INSENSITIVE_PREFIX_MATCH;
if (emote_lower.includes(term_lower)) if (isUpperChar(emote_name.charAt(termPosition)))
return NON_PREFIX_MATCH; return CAPITALIZED_SECTION_MATCH;
if (termPosition != -1)
return SUBSTRING_MATCH;
return NO_MATCH; return NO_MATCH;
} }
@ -911,24 +920,11 @@ export default class Input extends Module {
else return 0 - bIsEmoji + aIsEmoji; else return 0 - bIsEmoji + aIsEmoji;
} }
// Prefer case-sensitive prefix matches // Prefer highest match priority, then sort alphabetically
const aStartsWithInput = (a.match_type === EXACT_PREFIX_MATCH); if (a.match_type !== b.match_type)
const bStartsWithInput = (b.match_type === EXACT_PREFIX_MATCH); return b.match_type - a.match_type;
if (aStartsWithInput && bStartsWithInput) else
return locale.compare(aStr, bStr); return locale.compare(aStr, bStr);
else if (aStartsWithInput) return -1;
else if (bStartsWithInput) return 1;
// Else prefer case-insensitive prefix matches
const aStartsWithInputCI = (a.match_type === CASE_INSENSITIVE_PREFIX_MATCH);
const bStartsWithInputCI = (b.match_type === CASE_INSENSITIVE_PREFIX_MATCH);
if (aStartsWithInputCI && bStartsWithInputCI)
return localeCaseInsensitive.compare(aStr, bStr);
else if (aStartsWithInputCI) return -1;
else if (bStartsWithInputCI) return 1;
// Else alphabetize
return locale.compare(aStr, bStr);
} }
// Keep unsorted order for non-favorite items if prefix matching is not enabled. // Keep unsorted order for non-favorite items if prefix matching is not enabled.
@ -1028,7 +1024,8 @@ export default class Input extends Module {
return []; return [];
const results_usage = [], const results_usage = [],
results_starting = [], results_prefix = [],
results_capitalized = [],
results_other = [], results_other = [],
search = input.startsWith(':') ? input.slice(1) : input; search = input.startsWith(':') ? input.slice(1) : input;
@ -1048,18 +1045,21 @@ export default class Input extends Module {
if ( element.count > 0 ) if ( element.count > 0 )
results_usage.push(element); results_usage.push(element);
else if ( match_type > NON_PREFIX_MATCH ) else if ( match_type >= CASE_INSENSITIVE_PREFIX_MATCH )
results_starting.push(element); results_prefix.push(element);
else if ( match_type == CAPITALIZED_SECTION_MATCH )
results_capitalized.push(element);
else else
results_other.push(element); results_other.push(element);
} }
} }
results_usage.sort((a,b) => b.count - a.count); results_usage.sort((a,b) => b.count - a.count);
results_starting.sort((a,b) => locale.compare(a.replacement, b.replacement)); results_prefix.sort((a,b) => locale.compare(a.replacement, b.replacement));
results_capitalized.sort((a,b) => locale.compare(a.replacement, b.replacement));
results_other.sort((a,b) => locale.compare(a.replacement, b.replacement)); results_other.sort((a,b) => locale.compare(a.replacement, b.replacement));
return results_usage.concat(results_starting).concat(results_other); return results_usage.concat(results_prefix).concat(results_capitalized).concat(results_other);
} }