From 85f8665851c88df786986b35e2c75cec83862dd1 Mon Sep 17 00:00:00 2001 From: evykassirer Date: Mon, 4 Nov 2024 18:55:44 -0800 Subject: [PATCH] util: Move compare_a_b from user_sort and generalize it. --- web/src/settings_users.js | 7 ++++--- web/src/user_group_components.ts | 5 +++-- web/src/user_sort.ts | 10 +--------- web/src/util.ts | 9 +++++++++ web/tests/util.test.js | 26 ++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/web/src/settings_users.js b/web/src/settings_users.js index 2d964370cc..b3e0b86f32 100644 --- a/web/src/settings_users.js +++ b/web/src/settings_users.js @@ -22,6 +22,7 @@ import * as timerender from "./timerender"; import * as user_deactivation_ui from "./user_deactivation_ui"; import * as user_profile from "./user_profile"; 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 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 user_sort.compare_a_b(email(a), email(b)); + return util.compare_a_b(email(a), email(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 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) { - return user_sort.compare_a_b( + return util.compare_a_b( presence.last_active_date(a.user_id) || 0, presence.last_active_date(b.user_id) || 0, ); diff --git a/web/src/user_group_components.ts b/web/src/user_group_components.ts index d3a774c76a..9ac44ef421 100644 --- a/web/src/user_group_components.ts +++ b/web/src/user_group_components.ts @@ -5,6 +5,7 @@ import * as people from "./people"; import type {User} from "./people"; import type {UserGroup} from "./user_groups"; import * as user_sort from "./user_sort"; +import * as util from "./util"; 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 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 { @@ -95,7 +96,7 @@ export function sort_group_member_name(a: User | UserGroup, b: User | UserGroup) 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 { diff --git a/web/src/user_sort.ts b/web/src/user_sort.ts index 750b62ddb6..ecbf2f2dff 100644 --- a/web/src/user_sort.ts +++ b/web/src/user_sort.ts @@ -1,13 +1,5 @@ import type {User} from "./people"; - -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; -} +import {compare_a_b} from "./util"; export function sort_email(a: User, b: User): number { const email_a = a.delivery_email; diff --git a/web/src/util.ts b/web/src/util.ts index 78345526af..eaacb89808 100644 --- a/web/src/util.ts +++ b/web/src/util.ts @@ -488,3 +488,12 @@ export function the(items: T[] | JQuery): T { } return items[0]!; } + +export function compare_a_b(a: T, b: T): number { + if (a > b) { + return 1; + } else if (a === b) { + return 0; + } + return -1; +} diff --git a/web/tests/util.test.js b/web/tests/util.test.js index 9e7c082bcb..9744eb8e40 100644 --- a/web/tests/util.test.js +++ b/web/tests/util.test.js @@ -443,3 +443,29 @@ run_test("the", () => { // were previously typed wrong but not breaking the app. 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]); +});