diff --git a/src/libs/searchCountryOptions.js b/src/libs/searchCountryOptions.js deleted file mode 100644 index 9b0357a17a65..000000000000 --- a/src/libs/searchCountryOptions.js +++ /dev/null @@ -1,26 +0,0 @@ -import _ from 'lodash'; -import StringUtils from './StringUtils'; - -/** - * Searches the countries/states data and returns sorted results based on the search query - * @param {String} searchValue - * @param {Object[]} countriesData - An array of country data objects - * @returns {Object[]} An array of countries/states sorted based on the search query - */ -function searchCountryOptions(searchValue, countriesData) { - if (_.isEmpty(searchValue)) { - return countriesData; - } - - const trimmedSearchValue = StringUtils.sanitizeString(searchValue); - if (_.isEmpty(trimmedSearchValue)) { - return []; - } - - const filteredData = _.filter(countriesData, (country) => _.includes(country.searchValue, trimmedSearchValue)); - - // sort by country code - return _.sortBy(filteredData, (country) => (_.toLower(country.value) === trimmedSearchValue ? -1 : 1)); -} - -export default searchCountryOptions; diff --git a/src/libs/searchCountryOptions.ts b/src/libs/searchCountryOptions.ts new file mode 100644 index 000000000000..8fb1cc9c37f3 --- /dev/null +++ b/src/libs/searchCountryOptions.ts @@ -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;