typeahead: Rename compare_by_popularity() for clarity.

compare_by_popularity() actually does two things:
1. compare A and B by their popularity
2. then if needed, compare alphabetically to break ties

Someone reading the function name might not be clear about that detail.

I'm renaming this function to remove the "by_popularity" wording, which
can mislead readers. I also added comments to clarify the detail.

The goal in future diffs is to break this up and make it more self
explanatory.
This commit is contained in:
Trident Pancake
2023-01-08 15:43:32 -08:00
committed by Tim Abbott
parent da868d7e64
commit cb6c1c4f05
2 changed files with 6 additions and 4 deletions

View File

@@ -29,7 +29,7 @@ export function get_playground_info_for_languages(lang) {
export function sort_pygments_pretty_names_by_priority(generated_pygments_data) {
const priority_sorted_pygments_data = Object.keys(generated_pygments_data.langs).sort(
typeahead_helper.compare_by_popularity,
typeahead_helper.compare_language,
);
for (const alias of priority_sorted_pygments_data) {
const pretty_name = generated_pygments_data.langs[alias].pretty_name;

View File

@@ -259,7 +259,9 @@ export function sort_people_for_relevance(objs, current_stream_name, current_top
return objs;
}
export function compare_by_popularity(lang_a, lang_b) {
// This function compares two languages first by their popularity, then if
// there is a tie on popularity, then compare alphabetically to break the tie.
export function compare_language(lang_a, lang_b) {
const diff = pygments_data.langs[lang_b].priority - pygments_data.langs[lang_a].priority;
if (diff !== 0) {
return diff;
@@ -289,7 +291,7 @@ export function sort_languages(matches, query) {
const results = typeahead.triage(query, matches, (x) => x);
// Languages that start with the query
results.matches = results.matches.sort(compare_by_popularity);
results.matches = results.matches.sort(compare_language);
// Push exact matches to top.
const match_index = results.matches.indexOf(query);
@@ -299,7 +301,7 @@ export function sort_languages(matches, query) {
}
// Languages that have the query somewhere in their name
results.rest = results.rest.sort(compare_by_popularity);
results.rest = results.rest.sort(compare_language);
return retain_unique_language_aliases(results.matches.concat(results.rest));
}