From b01ce01e2d98110677bfe976d247cb11baa25e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Gon=C3=A7alves?= Date: Tue, 5 Sep 2023 15:20:28 +0100 Subject: [PATCH 1/4] Converted file from .js to .ts --- src/libs/{searchCountryOptions.js => searchCountryOptions.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/libs/{searchCountryOptions.js => searchCountryOptions.ts} (100%) diff --git a/src/libs/searchCountryOptions.js b/src/libs/searchCountryOptions.ts similarity index 100% rename from src/libs/searchCountryOptions.js rename to src/libs/searchCountryOptions.ts From 409ec7b1551457841d1358e2216851253144296a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Gon=C3=A7alves?= Date: Tue, 5 Sep 2023 17:27:39 +0100 Subject: [PATCH 2/4] Converted it to ts --- src/libs/searchCountryOptions.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libs/searchCountryOptions.ts b/src/libs/searchCountryOptions.ts index 9b0357a17a65..b257ac78f92c 100644 --- a/src/libs/searchCountryOptions.ts +++ b/src/libs/searchCountryOptions.ts @@ -1,13 +1,15 @@ 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) { +type CountryData = { + value: string; + keyForList: string; + text: string; + isSelected: boolean; + searchValue: string; +}; + +function searchCountryOptions(searchValue: string, countriesData: CountryData[]): CountryData[] { if (_.isEmpty(searchValue)) { return countriesData; } @@ -17,10 +19,9 @@ function searchCountryOptions(searchValue, countriesData) { return []; } - const filteredData = _.filter(countriesData, (country) => _.includes(country.searchValue, trimmedSearchValue)); + const filteredData = countriesData.filter((country) => country.searchValue.includes(trimmedSearchValue)); - // sort by country code - return _.sortBy(filteredData, (country) => (_.toLower(country.value) === trimmedSearchValue ? -1 : 1)); + return _.sortBy(filteredData, (country) => (country.value.toLowerCase() === trimmedSearchValue ? -1 : 1)); } export default searchCountryOptions; From 9ebafdd6eadc73be69f1bc9ccfb6f7d545bdbacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Gon=C3=A7alves?= Date: Tue, 5 Sep 2023 18:13:24 +0100 Subject: [PATCH 3/4] Removed lodash usage --- src/libs/searchCountryOptions.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libs/searchCountryOptions.ts b/src/libs/searchCountryOptions.ts index b257ac78f92c..ed6a584d2af2 100644 --- a/src/libs/searchCountryOptions.ts +++ b/src/libs/searchCountryOptions.ts @@ -1,4 +1,3 @@ -import _ from 'lodash'; import StringUtils from './StringUtils'; type CountryData = { @@ -10,18 +9,26 @@ type CountryData = { }; function searchCountryOptions(searchValue: string, countriesData: CountryData[]): CountryData[] { - if (_.isEmpty(searchValue)) { + if (!searchValue.trim()) { return countriesData; } - const trimmedSearchValue = StringUtils.sanitizeString(searchValue); - if (_.isEmpty(trimmedSearchValue)) { + const trimmedSearchValue = StringUtils.sanitizeString(searchValue.trim()); + if (!trimmedSearchValue) { return []; } const filteredData = countriesData.filter((country) => country.searchValue.includes(trimmedSearchValue)); - return _.sortBy(filteredData, (country) => (country.value.toLowerCase() === trimmedSearchValue ? -1 : 1)); + return filteredData.sort((a, b) => { + if (a.value.toLowerCase() === trimmedSearchValue) { + return -1; + } + if (b.value.toLowerCase() === trimmedSearchValue) { + return 1; + } + return 0; + }); } export default searchCountryOptions; From 63eb2664c3ecb197c659e6ca23025494c20c723d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Gon=C3=A7alves?= Date: Mon, 11 Sep 2023 15:47:55 +0100 Subject: [PATCH 4/4] Removed unnecessary trim function and put back comments in place --- src/libs/searchCountryOptions.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libs/searchCountryOptions.ts b/src/libs/searchCountryOptions.ts index ed6a584d2af2..8fb1cc9c37f3 100644 --- a/src/libs/searchCountryOptions.ts +++ b/src/libs/searchCountryOptions.ts @@ -8,12 +8,17 @@ type CountryData = { 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.trim()) { + if (!searchValue) { return countriesData; } - const trimmedSearchValue = StringUtils.sanitizeString(searchValue.trim()); + const trimmedSearchValue = StringUtils.sanitizeString(searchValue); if (!trimmedSearchValue) { return []; }