Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import localeCompare from './LocaleCompare';
import {translateLocal} from './Localize';
import {filterObject} from './ObjectUtils';
import {getDisplayNameOrDefault} from './PersonalDetailsUtils';
import StringUtils from './StringUtils';

let allCards: OnyxValues[typeof ONYXKEYS.CARD_LIST] = {};
Onyx.connect({
Expand Down Expand Up @@ -271,11 +272,17 @@ function sortCardsByCardholderName(cards: Card[], personalDetails: OnyxEntry<Per
}

function filterCardsByPersonalDetails(card: Card, searchQuery: string, personalDetails?: PersonalDetailsList) {
const cardTitle = card.nameValuePairs?.cardTitle?.toLowerCase() ?? '';
const lastFourPAN = card?.lastFourPAN?.toLowerCase() ?? '';
const accountLogin = personalDetails?.[card.accountID ?? CONST.DEFAULT_NUMBER_ID]?.login?.toLowerCase() ?? '';
const accountName = personalDetails?.[card.accountID ?? CONST.DEFAULT_NUMBER_ID]?.displayName?.toLowerCase() ?? '';
return cardTitle.includes(searchQuery) || lastFourPAN.includes(searchQuery) || accountLogin.includes(searchQuery) || accountName.includes(searchQuery);
const normalizedSearchQuery = StringUtils.normalize(searchQuery.toLowerCase());
const cardTitle = StringUtils.normalize(card.nameValuePairs?.cardTitle?.toLowerCase() ?? '');
const lastFourPAN = StringUtils.normalize(card?.lastFourPAN?.toLowerCase() ?? '');
const accountLogin = StringUtils.normalize(personalDetails?.[card.accountID ?? CONST.DEFAULT_NUMBER_ID]?.login?.toLowerCase() ?? '');
const accountName = StringUtils.normalize(personalDetails?.[card.accountID ?? CONST.DEFAULT_NUMBER_ID]?.displayName?.toLowerCase() ?? '');
return (
cardTitle.includes(normalizedSearchQuery) ||
lastFourPAN.includes(normalizedSearchQuery) ||
accountLogin.includes(normalizedSearchQuery) ||
accountName.includes(normalizedSearchQuery)
);
}

function getCardFeedIcon(cardFeed: CompanyCardFeed | typeof CONST.EXPENSIFY_CARD.BANK, illustrations: IllustrationsType): IconAsset {
Expand Down
17 changes: 17 additions & 0 deletions src/libs/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ function normalizeAccents(text: string) {
return text.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}

/**
* Normalize a string by:
* - removing diacritical marks
* - Removing non-alphabetic and non-latin characters from a string
* - Removing invisible characters
* - normalizing space-like characters into normal spaces
* - collapsing whitespaces
* - trimming
*/
function normalize(text: string): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this reuse some of the other normalize functions? Like for diacritics?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does reuse sanitizeString and removeInvisibleCharacters.

I agree there's kind of a lot of overlap here that should probably be cleaned up at some point. Don't really want to do it in the scope of a deploy blocker fix though. I feel like long-term we'll want a really fast fuzzy matcher that handles normalization internally.

@MonilBhavsar MonilBhavsar May 9, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why sanitizeString is being used, as it caused a bug #61752

return removeInvisibleCharacters(sanitizeString(text))
.replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, ' ') // space-like -> ' '
.replace(/\s+/g, ' ') // collapse spaces
.trim();
}

/**
* Replace all CRLF with LF
* @param value - The input string
Expand Down Expand Up @@ -125,6 +141,7 @@ export default {
sanitizeString,
isEmptyString,
removeInvisibleCharacters,
normalize,
normalizeAccents,
normalizeCRLF,
lineBreaksToSpaces,
Expand Down
11 changes: 7 additions & 4 deletions src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {isPersonalDetailsReady, sortAlphabetically} from '@libs/OptionsListUtils
import {getAccountIDsByLogins, getDisplayNameOrDefault, getPersonalDetailsByIDs} from '@libs/PersonalDetailsUtils';
import {getMemberAccountIDsForWorkspace, isDeletedPolicyEmployee, isExpensifyTeam, isPaidGroupPolicy, isPolicyAdmin as isPolicyAdminUtils} from '@libs/PolicyUtils';
import {getDisplayNameForParticipant} from '@libs/ReportUtils';
import StringUtils from '@libs/StringUtils';
import {convertPolicyEmployeesToApprovalWorkflows, updateWorkflowDataOnApproverRemoval} from '@libs/WorkflowUtils';
import {close} from '@userActions/Modal';
import {dismissAddedWithPrimaryLoginMessages} from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -491,10 +492,12 @@ function WorkspaceMembersPage({personalDetails, route, policy, currentUserPerson
isPolicyAdmin,
]);

const filterMember = useCallback(
(memberOption: MemberOption, searchQuery: string) => !!memberOption.text?.toLowerCase().includes(searchQuery) || !!memberOption.alternateText?.toLowerCase().includes(searchQuery),
[],
);
const filterMember = useCallback((memberOption: MemberOption, searchQuery: string) => {
const memberText = StringUtils.normalize(memberOption.text?.toLowerCase() ?? '');
const alternateText = StringUtils.normalize(memberOption.alternateText?.toLowerCase() ?? '');
const normalizedSearchQuery = StringUtils.normalize(searchQuery);
return memberText.includes(normalizedSearchQuery) || alternateText.includes(normalizedSearchQuery);
}, []);
const sortMembers = useCallback((memberOptions: MemberOption[]) => sortAlphabetically(memberOptions, 'text'), []);
const [inputValue, setInputValue, filteredData] = useSearchResults(data, filterMember, sortMembers);

Expand Down
8 changes: 6 additions & 2 deletions src/pages/workspace/categories/WorkspaceCategoriesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types';
import {getConnectedIntegration, getCurrentConnectionName, hasAccountingConnections, shouldShowSyncError} from '@libs/PolicyUtils';
import StringUtils from '@libs/StringUtils';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import {close} from '@userActions/Modal';
import {clearCategoryErrors, deleteWorkspaceCategories, downloadCategoriesCSV, openPolicyCategoriesPage, setWorkspaceCategoryEnabled} from '@userActions/Policy/Category';
Expand Down Expand Up @@ -132,7 +133,7 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
acc.push({
text: value.name,
keyForList: value.name,
isSelected: !!selectedCategories.includes(value.name) && canSelectMultiple,
isSelected: selectedCategories.includes(value.name) && canSelectMultiple,
isDisabled,
pendingAction: value.pendingAction,
errors: value.errors ?? undefined,
Expand All @@ -151,7 +152,10 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
}, [policyCategories, isOffline, selectedCategories, canSelectMultiple, translate, updateWorkspaceRequiresCategory]);

const filterCategory = useCallback((categoryOption: PolicyOption, searchInput: string) => {
return !!categoryOption.text?.toLowerCase().includes(searchInput) || !!categoryOption.alternateText?.toLowerCase().includes(searchInput);
const categoryText = StringUtils.normalize(categoryOption.text?.toLowerCase() ?? '');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from #72615, we should use tokenizedSearch here for better matching result

const alternateText = StringUtils.normalize(categoryOption.alternateText?.toLowerCase() ?? '');
const normalizedSearchInput = StringUtils.normalize(searchInput);
return categoryText.includes(normalizedSearchInput) || alternateText.includes(normalizedSearchInput);
}, []);
const sortCategories = useCallback((data: PolicyOption[]) => {
return lodashSortBy(data, 'text', localeCompare) as PolicyOption[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import goBackFromWorkspaceCentralScreen from '@libs/Navigation/helpers/goBackFro
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {getDistanceRateCustomUnit} from '@libs/PolicyUtils';
import StringUtils from '@libs/StringUtils';
import type {WorkspaceSplitNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import ButtonWithDropdownMenu from '@src/components/ButtonWithDropdownMenu';
Expand Down Expand Up @@ -178,7 +179,11 @@ function PolicyDistanceRatesPage({
[customUnitRates, translate, customUnit, selectedDistanceRates, canSelectMultiple, policy?.pendingAction, updateDistanceRateEnabled],
);

const filterRate = useCallback((rate: RateForList, searchInput: string) => !!rate.text?.toLowerCase().includes(searchInput.toLowerCase()), []);
const filterRate = useCallback((rate: RateForList, searchInput: string) => {
const rateText = StringUtils.normalize(rate.text?.toLowerCase() ?? '');
const normalizedSearchInput = StringUtils.normalize(searchInput.toLowerCase());
return rateText.includes(normalizedSearchInput);
}, []);
const sortRates = useCallback((rates: RateForList[]) => rates.sort((a, b) => (a.rate ?? 0) - (b.rate ?? 0)), []);
const [inputValue, setInputValue, filteredDistanceRatesList] = useSearchResults(distanceRatesList, filterRate, sortRates);

Expand Down
7 changes: 6 additions & 1 deletion src/pages/workspace/perDiem/WorkspacePerDiemPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavig
import type {WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types';
import {hasEnabledOptions} from '@libs/OptionsListUtils';
import {getPerDiemCustomUnit} from '@libs/PolicyUtils';
import StringUtils from '@libs/StringUtils';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import {openExternalLink} from '@userActions/Link';
import {turnOffMobileSelectionMode} from '@userActions/MobileSelectionMode';
Expand Down Expand Up @@ -195,7 +196,11 @@ function WorkspacePerDiemPage({route}: WorkspacePerDiemPageProps) {
[allSubRates, selectedPerDiem, canSelectMultiple, styles.flex2, styles.alignItemsStart, styles.textSupporting, styles.label, styles.pl2, styles.alignSelfEnd],
);

const filterRate = useCallback((rate: PolicyOption, searchInput: string) => !!rate.text?.toLowerCase().includes(searchInput), []);
const filterRate = useCallback((rate: PolicyOption, searchInput: string) => {
const rateText = StringUtils.normalize(rate.text?.toLowerCase() ?? '');
const normalizedSearchInput = StringUtils.normalize(searchInput.toLowerCase());
return rateText.includes(normalizedSearchInput);
}, []);
const sortRates = useCallback((rates: PolicyOption[]) => lodashSortBy(rates, 'text', localeCompare) as PolicyOption[], []);
const [inputValue, setInputValue, filteredSubRatesList] = useSearchResults(subRatesList, filterRate, sortRates);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {hasAccountingConnections as hasAccountingConnectionsPolicyUtils} from '@libs/PolicyUtils';
import {getReportFieldKey} from '@libs/ReportUtils';
import StringUtils from '@libs/StringUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
Expand Down Expand Up @@ -138,7 +139,11 @@ function ReportFieldsListValuesPage({
[canSelectMultiple, disabledListValues, listValues, selectedValues, translate, updateReportFieldListValueEnabled],
);

const filterListValue = useCallback((item: ValueListItem, searchInput: string) => !!item.text?.toLowerCase().includes(searchInput.toLowerCase()), []);
const filterListValue = useCallback((item: ValueListItem, searchInput: string) => {
const itemText = StringUtils.normalize(item.text?.toLowerCase() ?? '');
const normalizedSearchInput = StringUtils.normalize(searchInput.toLowerCase());
return itemText.includes(normalizedSearchInput);
}, []);
const sortListValues = useCallback((values: ValueListItem[]) => values.sort((a, b) => localeCompare(a.value, b.value)), []);
const [inputValue, setInputValue, filteredListValues] = useSearchResults(data, filterListValue, sortListValues);

Expand Down
8 changes: 7 additions & 1 deletion src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
isMultiLevelTags as isMultiLevelTagsPolicyUtils,
shouldShowSyncError,
} from '@libs/PolicyUtils';
import StringUtils from '@libs/StringUtils';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import {close} from '@userActions/Modal';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -209,7 +210,12 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
}));
}, [isMultiLevelTags, policyTagLists, selectedTags, canSelectMultiple, translate, updateWorkspaceRequiresTag, updateWorkspaceTagEnabled]);

const filterTag = useCallback((tag: TagListItem, searchInput: string) => !!tag.text?.toLowerCase().includes(searchInput) || !!tag.value?.toLowerCase().includes(searchInput), []);
const filterTag = useCallback((tag: TagListItem, searchInput: string) => {
const tagText = StringUtils.normalize(tag.text?.toLowerCase() ?? '');
const tagValue = StringUtils.normalize(tag.value?.toLowerCase() ?? '');
const normalizeSearchInput = StringUtils.normalize(searchInput.toLowerCase());
return tagText.includes(normalizeSearchInput) || tagValue.includes(normalizeSearchInput);
}, []);
const sortTags = useCallback((tags: TagListItem[]) => lodashSortBy(tags, 'value', localeCompare) as TagListItem[], []);
const [inputValue, setInputValue, filteredTagList] = useSearchResults(tagList, filterTag, sortTags);

Expand Down
8 changes: 7 additions & 1 deletion src/pages/workspace/tags/WorkspaceViewTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {getCleanedTagName, getTagListName, hasDependentTags as hasDependentTagsPolicyUtils, isMultiLevelTags as isMultiLevelTagsPolicyUtils} from '@libs/PolicyUtils';
import StringUtils from '@libs/StringUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
Expand Down Expand Up @@ -131,7 +132,12 @@ function WorkspaceViewTagsPage({route}: WorkspaceViewTagsProps) {
[currentPolicyTag?.tags, selectedTags, canSelectMultiple, translate, updateWorkspaceTagEnabled],
);

const filterTag = useCallback((tag: TagListItem, searchInput: string) => !!tag.text?.toLowerCase().includes(searchInput) || !!tag.value?.toLowerCase().includes(searchInput), []);
const filterTag = useCallback((tag: TagListItem, searchInput: string) => {
const tagText = StringUtils.normalize(tag.text?.toLowerCase() ?? '');
const tagValue = StringUtils.normalize(tag.text?.toLowerCase() ?? '');
const normalizedSearchInput = StringUtils.normalize(searchInput.toLowerCase() ?? '');
return tagText.includes(normalizedSearchInput) || tagValue.includes(normalizedSearchInput);
}, []);
const sortTags = useCallback((tags: TagListItem[]) => tags.sort((tagA, tagB) => localeCompare(tagA.value, tagB.value)), []);
const [inputValue, setInputValue, filteredTagList] = useSearchResults(tagList, filterTag, sortTags);

Expand Down
8 changes: 5 additions & 3 deletions src/pages/workspace/taxes/WorkspaceTaxesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
hasAccountingConnections as hasAccountingConnectionsPolicyUtils,
shouldShowSyncError,
} from '@libs/PolicyUtils';
import StringUtils from '@libs/StringUtils';
import type {WorkspaceSplitNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
Expand Down Expand Up @@ -184,9 +185,10 @@ function WorkspaceTaxesPage({
}, [policy, textForDefault, selectedTaxesIDs, canSelectMultiple, translate, updateWorkspaceTaxEnabled]);

const filterTax = useCallback((tax: ListItem, searchInput: string) => {
const taxName = tax.text?.toLowerCase() ?? '';
const taxAlternateText = tax.alternateText?.toLowerCase() ?? '';
return taxName.includes(searchInput) || taxAlternateText.includes(searchInput);
const taxName = StringUtils.normalize(tax.text?.toLowerCase() ?? '');
const taxAlternateText = StringUtils.normalize(tax.alternateText?.toLowerCase() ?? '');
const normalizedSearchInput = StringUtils.normalize(searchInput.toLowerCase() ?? '');
return taxName.includes(normalizedSearchInput) || taxAlternateText.includes(normalizedSearchInput);
}, []);
const sortTaxes = useCallback((taxes: ListItem[]) => {
return taxes.sort((a, b) => {
Expand Down