Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'searchCountryOptions.js' lib to TypeScript #24804 #26811

Merged
merged 4 commits into from
Sep 14, 2023
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
26 changes: 0 additions & 26 deletions src/libs/searchCountryOptions.js

This file was deleted.

39 changes: 39 additions & 0 deletions src/libs/searchCountryOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import StringUtils from './StringUtils';

type CountryData = {
value: string;
keyForList: string;
text: string;
isSelected: boolean;
searchValue: string;
};

/**
* Searches the countries/states data and returns sorted results based on the search query
* @param countriesData - An array of country data objects
* @returns An array of countries/states sorted based on the search query
*/
function searchCountryOptions(searchValue: string, countriesData: CountryData[]): CountryData[] {
arthurmfgtab marked this conversation as resolved.
Show resolved Hide resolved
if (!searchValue) {
return countriesData;
}

const trimmedSearchValue = StringUtils.sanitizeString(searchValue);
if (!trimmedSearchValue) {
return [];
}

const filteredData = countriesData.filter((country) => country.searchValue.includes(trimmedSearchValue));

return filteredData.sort((a, b) => {
if (a.value.toLowerCase() === trimmedSearchValue) {
return -1;
}
if (b.value.toLowerCase() === trimmedSearchValue) {
return 1;
}
return 0;
});
}

export default searchCountryOptions;
Loading