Skip to content

Commit

Permalink
Merge pull request #26811 from arthurmfgtab/ts-migration/searchCountr…
Browse files Browse the repository at this point in the history
…yOptions

[TS migration] Migrate 'searchCountryOptions.js' lib to TypeScript #24804
  • Loading branch information
Joel Bettner authored Sep 14, 2023
2 parents c413f70 + 63eb266 commit 954c66d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
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[] {
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;

0 comments on commit 954c66d

Please sign in to comment.