util: Move compare_a_b from user_sort and generalize it.

This commit is contained in:
evykassirer 2024-11-04 18:55:44 -08:00 committed by Tim Abbott
parent 01b1a51a86
commit 85f8665851
5 changed files with 43 additions and 14 deletions

View File

@ -22,6 +22,7 @@ import * as timerender from "./timerender";
import * as user_deactivation_ui from "./user_deactivation_ui"; import * as user_deactivation_ui from "./user_deactivation_ui";
import * as user_profile from "./user_profile"; import * as user_profile from "./user_profile";
import * as user_sort from "./user_sort"; import * as user_sort from "./user_sort";
import * as util from "./util";
export const active_user_list_dropdown_widget_name = "active_user_list_select_user_role"; export const active_user_list_dropdown_widget_name = "active_user_list_select_user_role";
export const deactivated_user_list_dropdown_widget_name = "deactivated_user_list_select_user_role"; export const deactivated_user_list_dropdown_widget_name = "deactivated_user_list_select_user_role";
@ -54,7 +55,7 @@ function sort_bot_email(a, b) {
return (bot.display_email || "").toLowerCase(); return (bot.display_email || "").toLowerCase();
} }
return user_sort.compare_a_b(email(a), email(b)); return util.compare_a_b(email(a), email(b));
} }
function sort_bot_owner(a, b) { function sort_bot_owner(a, b) {
@ -62,11 +63,11 @@ function sort_bot_owner(a, b) {
return (bot.bot_owner_full_name || "").toLowerCase(); return (bot.bot_owner_full_name || "").toLowerCase();
} }
return user_sort.compare_a_b(owner_name(a), owner_name(b)); return util.compare_a_b(owner_name(a), owner_name(b));
} }
function sort_last_active(a, b) { function sort_last_active(a, b) {
return user_sort.compare_a_b( return util.compare_a_b(
presence.last_active_date(a.user_id) || 0, presence.last_active_date(a.user_id) || 0,
presence.last_active_date(b.user_id) || 0, presence.last_active_date(b.user_id) || 0,
); );

View File

@ -5,6 +5,7 @@ import * as people from "./people";
import type {User} from "./people"; import type {User} from "./people";
import type {UserGroup} from "./user_groups"; import type {UserGroup} from "./user_groups";
import * as user_sort from "./user_sort"; import * as user_sort from "./user_sort";
import * as util from "./util";
export let active_group_id: number | undefined; export let active_group_id: number | undefined;
@ -77,7 +78,7 @@ export function sort_group_member_email(a: User | UserGroup, b: User | UserGroup
return 1; return 1;
} }
return user_sort.compare_a_b(a.name.toLowerCase(), b.name.toLowerCase()); return util.compare_a_b(a.name.toLowerCase(), b.name.toLowerCase());
} }
export function sort_group_member_name(a: User | UserGroup, b: User | UserGroup): number { export function sort_group_member_name(a: User | UserGroup, b: User | UserGroup): number {
@ -95,7 +96,7 @@ export function sort_group_member_name(a: User | UserGroup, b: User | UserGroup)
b_name = b.name; b_name = b.name;
} }
return user_sort.compare_a_b(a_name.toLowerCase(), b_name.toLowerCase()); return util.compare_a_b(a_name.toLowerCase(), b_name.toLowerCase());
} }
export function build_group_member_matcher(query: string): (member: User | UserGroup) => boolean { export function build_group_member_matcher(query: string): (member: User | UserGroup) => boolean {

View File

@ -1,13 +1,5 @@
import type {User} from "./people"; import type {User} from "./people";
import {compare_a_b} from "./util";
export function compare_a_b(a: number | string, b: number | string): number {
if (a > b) {
return 1;
} else if (a === b) {
return 0;
}
return -1;
}
export function sort_email(a: User, b: User): number { export function sort_email(a: User, b: User): number {
const email_a = a.delivery_email; const email_a = a.delivery_email;

View File

@ -488,3 +488,12 @@ export function the<T>(items: T[] | JQuery<T>): T {
} }
return items[0]!; return items[0]!;
} }
export function compare_a_b<T>(a: T, b: T): number {
if (a > b) {
return 1;
} else if (a === b) {
return 0;
}
return -1;
}

View File

@ -443,3 +443,29 @@ run_test("the", () => {
// were previously typed wrong but not breaking the app. // were previously typed wrong but not breaking the app.
assert.equal(util.the([]), undefined); assert.equal(util.the([]), undefined);
}); });
run_test("compare_a_b", () => {
const user1 = {
id: 1,
name: "sally",
};
const user2 = {
id: 2,
name: "jenny",
};
const user3 = {
id: 3,
name: "max",
};
const user4 = {
id: 4,
name: "max",
};
const unsorted = [user2, user1, user4, user3];
const sorted_by_id = [...unsorted].sort((a, b) => util.compare_a_b(a.id, b.id));
assert.deepEqual(sorted_by_id, [user1, user2, user3, user4]);
const sorted_by_name = [...unsorted].sort((a, b) => util.compare_a_b(a.name, b.name));
assert.deepEqual(sorted_by_name, [user2, user4, user3, user1]);
});