Skip to content

Commit 6e81f81

Browse files
committed
chore(location-service): extract search by Iso and ID logic
1 parent 9375fb8 commit 6e81f81

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

services/location.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import {
1010
getSubRegionsProvider,
1111
} from 'services/country';
1212

13+
const findByIso = (list, iso) => list?.find((item) => item?.iso === iso);
14+
const findById = (list, idPrefix) =>
15+
list?.find((item) => item?.id.startsWith(idPrefix));
16+
1317
export const countryConfig = {
1418
adm0: async ({ adm0 }) => {
1519
const { data: countries } = await getCountriesProvider();
16-
const country = countries?.find((countryItem) => countryItem?.iso === adm0);
20+
const country = findByIso(countries, adm0);
1721
const { name, ...props } = country || {};
1822

1923
return {
@@ -30,10 +34,8 @@ export const countryConfig = {
3034
const { data: countries } = countriesData || {};
3135
const { data: regions } = regionsData || {};
3236

33-
const country = countries?.find((countryItem) => countryItem?.iso === adm0);
34-
const region = regions?.find((regionItem) =>
35-
regionItem?.id.startsWith(`${adm0}.${adm1}_`)
36-
);
37+
const country = findByIso(countries, adm0);
38+
const region = findById(regions, `${adm0}.${adm1}_`);
3739

3840
const { name, ...props } = region;
3941

@@ -53,13 +55,9 @@ export const countryConfig = {
5355
const { data: regions } = regionsData || {};
5456
const { data: subRegions } = subRegionsData || {};
5557

56-
const country = countries?.find((countryItem) => countryItem?.iso === adm0);
57-
const region = regions?.find((regionItem) =>
58-
regionItem?.id.startsWith(`${adm0}.${adm1}_`)
59-
);
60-
const subRegion = subRegions?.find((subRegionItem) =>
61-
subRegionItem?.id.startsWith(`${adm0}.${adm1}.${adm2}_`)
62-
);
58+
const country = findByIso(countries, adm0);
59+
const region = findById(regions, `${adm0}.${adm1}_`);
60+
const subRegion = findById(subRegions, `${adm0}.${adm1}.${adm2}_`);
6361

6462
const { name, ...props } = subRegion;
6563

0 commit comments

Comments
 (0)