Skip to content

Commit

Permalink
replace localeCompare with primitive comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
kacper-mikolajczak committed Feb 14, 2024
1 parent f268c39 commit 684d496
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/libs/GroupChatUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {Report} from '@src/types/onyx';
import * as ReportUtils from './ReportUtils';
import stringCompare from './stringCompare';

/**
* Returns the report name if the report is a group chat
Expand All @@ -10,7 +11,7 @@ function getGroupChatName(report: Report): string | undefined {

return participants
.map((participant) => ReportUtils.getDisplayNameForParticipant(participant, isMultipleParticipantReport))
.sort((first, second) => first?.localeCompare(second ?? '') ?? 0)
.sort((first, second) => stringCompare(first ?? '', second ?? '') ?? 0)
.filter(Boolean)
.join(', ');
}
Expand Down
3 changes: 2 additions & 1 deletion src/libs/KeyboardShortcut/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Str from 'expensify-common/lib/str';
import * as KeyCommand from 'react-native-key-command';
import getOperatingSystem from '@libs/getOperatingSystem';
import stringCompare from '@libs/stringCompare';
import CONST from '@src/CONST';
import bindHandlerToKeydownEvent from './bindHandlerToKeydownEvent';

Expand Down Expand Up @@ -32,7 +33,7 @@ type Shortcut = {
const documentedShortcuts: Record<string, Shortcut> = {};

function getDocumentedShortcuts(): Shortcut[] {
return Object.values(documentedShortcuts).sort((a, b) => a.displayName.localeCompare(b.displayName));
return Object.values(documentedShortcuts).sort((a, b) => stringCompare(a.displayName, b.displayName));
}

const keyInputEnter = KeyCommand?.constants?.keyInputEnter?.toString() ?? 'keyInputEnter';
Expand Down
5 changes: 3 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as PersonalDetailsUtils from './PersonalDetailsUtils';
import * as PolicyUtils from './PolicyUtils';
import * as ReportActionsUtils from './ReportActionsUtils';
import type {LastVisibleMessage} from './ReportActionsUtils';
import stringCompare from './stringCompare';
import * as TransactionUtils from './TransactionUtils';
import * as Url from './Url';
import * as UserUtils from './UserUtils';
Expand Down Expand Up @@ -1296,7 +1297,7 @@ function getIconsForParticipants(participants: number[], personalDetails: OnyxCo

const sortedParticipantDetails = participantDetails.sort((first, second) => {
// First sort by displayName/login
const displayNameLoginOrder = first[1].localeCompare(second[1]);
const displayNameLoginOrder = stringCompare(first[1], second[1]);
if (displayNameLoginOrder !== 0) {
return displayNameLoginOrder;
}
Expand Down Expand Up @@ -1545,7 +1546,7 @@ function getDisplayNamesWithTooltips(
})
.sort((first, second) => {
// First sort by displayName/login
const displayNameLoginOrder = first.displayName.localeCompare(second.displayName);
const displayNameLoginOrder = stringCompare(first.displayName, second.displayName);
if (displayNameLoginOrder !== 0) {
return displayNameLoginOrder;
}
Expand Down
11 changes: 6 additions & 5 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as OptionsListUtils from './OptionsListUtils';
import * as PersonalDetailsUtils from './PersonalDetailsUtils';
import * as ReportActionsUtils from './ReportActionsUtils';
import * as ReportUtils from './ReportUtils';
import stringCompare from './stringCompare';
import * as TaskUtils from './TaskUtils';
import * as UserUtils from './UserUtils';

Expand Down Expand Up @@ -200,20 +201,20 @@ function getOrderedReportIDs(
});

// Sort each group of reports accordingly
pinnedAndGBRReports.sort((a, b) => (a?.displayName && b?.displayName ? a.displayName.toLowerCase().localeCompare(b.displayName.toLowerCase()) : 0));
draftReports.sort((a, b) => (a?.displayName && b?.displayName ? a.displayName.toLowerCase().localeCompare(b.displayName.toLowerCase()) : 0));
pinnedAndGBRReports.sort((a, b) => (a?.displayName && b?.displayName ? stringCompare(a.displayName, b.displayName) : 0));
draftReports.sort((a, b) => (a?.displayName && b?.displayName ? stringCompare(a.displayName, b.displayName) : 0));

if (isInDefaultMode) {
nonArchivedReports.sort((a, b) => {
const compareDates = a?.lastVisibleActionCreated && b?.lastVisibleActionCreated ? compareStringDates(b.lastVisibleActionCreated, a.lastVisibleActionCreated) : 0;
const compareDisplayNames = a?.displayName && b?.displayName ? a.displayName.toLowerCase().localeCompare(b.displayName.toLowerCase()) : 0;
const compareDisplayNames = a?.displayName && b?.displayName ? stringCompare(a.displayName, b.displayName) : 0;
return compareDates || compareDisplayNames;
});
// For archived reports ensure that most recent reports are at the top by reversing the order
archivedReports.sort((a, b) => (a?.lastVisibleActionCreated && b?.lastVisibleActionCreated ? compareStringDates(b.lastVisibleActionCreated, a.lastVisibleActionCreated) : 0));
} else {
nonArchivedReports.sort((a, b) => (a?.displayName && b?.displayName ? a.displayName.toLowerCase().localeCompare(b.displayName.toLowerCase()) : 0));
archivedReports.sort((a, b) => (a?.displayName && b?.displayName ? a.displayName.toLowerCase().localeCompare(b.displayName.toLowerCase()) : 0));
nonArchivedReports.sort((a, b) => (a?.displayName && b?.displayName ? stringCompare(a.displayName, b.displayName) : 0));
archivedReports.sort((a, b) => (a?.displayName && b?.displayName ? stringCompare(a.displayName, b.displayName) : 0));
}

// Now that we have all the reports grouped and sorted, they must be flattened into an array and only return the reportID.
Expand Down
18 changes: 18 additions & 0 deletions src/libs/stringCompare/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Compare two strings in a case-insensitive manner. It is a performance fallback for the built-in String.localeCompare method in Hermes.
* @param a - The first string to compare
* @param b - The second string to compare
* @returns - `1` if first string takes precedence, `-1` if second string takes precedence, `0` if they are equal
*/
function stringCompare(a: string, b: string): number {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}

// eslint-disable-next-line import/prefer-default-export
export default stringCompare;
11 changes: 11 additions & 0 deletions src/libs/stringCompare/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Compare two strings in a case-insensitive manner. It is a performance fallback for Hermes engine. For other engines, it is a wrapper around the built-in localeCompare method.
* @param a - The first string to compare
* @param b - The second string to compare
* @returns - `1` if first string takes precedence, `-1` if second string takes precedence, `0` if they are equal
*/
function stringCompare(a: string, b: string): number {
return a.localeCompare(b);
}

export default stringCompare;

0 comments on commit 684d496

Please sign in to comment.