@@ -10,10 +10,14 @@ import {
10
10
getSubRegionsProvider ,
11
11
} from 'services/country' ;
12
12
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
+
13
17
export const countryConfig = {
14
18
adm0 : async ( { adm0 } ) => {
15
19
const { data : countries } = await getCountriesProvider ( ) ;
16
- const country = countries ?. find ( ( countryItem ) => countryItem ?. iso === adm0 ) ;
20
+ const country = findByIso ( countries , adm0 ) ;
17
21
const { name, ...props } = country || { } ;
18
22
19
23
return {
@@ -30,10 +34,8 @@ export const countryConfig = {
30
34
const { data : countries } = countriesData || { } ;
31
35
const { data : regions } = regionsData || { } ;
32
36
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 } _` ) ;
37
39
38
40
const { name, ...props } = region ;
39
41
@@ -53,13 +55,9 @@ export const countryConfig = {
53
55
const { data : regions } = regionsData || { } ;
54
56
const { data : subRegions } = subRegionsData || { } ;
55
57
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 } _` ) ;
63
61
64
62
const { name, ...props } = subRegion ;
65
63
0 commit comments