@@ -63,12 +63,13 @@ type MapBoxQueryOptions<T extends BaseMapBoxGeocodingOptions> = {
63
63
[ K in keyof Omit < T , "countries" | "types" > ] : T [ K ] ;
64
64
} & MapBoxAdjustedGeocodingParams ;
65
65
66
- const RELEVANT_FEATURE_TYPES = [ "postcode" , "place" , "region" , "country" ] ;
66
+ const DEFAULT_RELEVANT_FEATURE_TYPES = [ "postcode" , "place" , "region" , "country" ] ;
67
67
const NA_COUNTRIES = [ "United States" , "Canada" , "Mexico" ] ;
68
68
const NA_ABBREVIATIONS = [ "US-" , "CA-" , "MX-" ] ;
69
69
70
- export function findBestFeature ( collection : MapBoxFeatureCollection ) : MapBoxFeature | null {
71
- const relevantFeatures = collection . features . filter ( feature => RELEVANT_FEATURE_TYPES . some ( type => feature . place_type . includes ( type ) ) ) ;
70
+ export function findBestFeature ( collection : MapBoxFeatureCollection , relevantTypes ?: MapBoxFeatureType [ ] ) : MapBoxFeature | null {
71
+ const types = relevantTypes ?? DEFAULT_RELEVANT_FEATURE_TYPES ;
72
+ const relevantFeatures = collection . features . filter ( feature => types . some ( type => feature . place_type . includes ( type ) ) ) ;
72
73
const placeFeature = relevantFeatures . find ( feature => feature . place_type . includes ( "place" ) ) ?? ( relevantFeatures . find ( feature => feature . place_type . includes ( "postcode" ) ) ?? undefined ) ;
73
74
if ( placeFeature !== undefined ) {
74
75
return placeFeature ;
@@ -84,14 +85,15 @@ export function findBestFeature(collection: MapBoxFeatureCollection): MapBoxFeat
84
85
return null ;
85
86
}
86
87
87
- export function textForMapboxFeature ( feature : MapBoxFeature ) : string {
88
+ export function textForMapboxFeature ( feature : MapBoxFeature , relevantTypes ?: MapBoxFeatureType [ ] ) : string {
89
+ const types = relevantTypes ?? DEFAULT_RELEVANT_FEATURE_TYPES ;
88
90
const pieces : string [ ] = [ ] ;
89
91
if ( feature . text ) {
90
92
pieces . push ( feature . text ) ;
91
93
}
92
94
feature . context . forEach ( item => {
93
95
const itemType = item . id . split ( "." ) [ 0 ] ;
94
- if ( ! RELEVANT_FEATURE_TYPES . includes ( itemType ) ) {
96
+ if ( ! types . includes ( itemType ) ) {
95
97
return ;
96
98
}
97
99
let text = null as string | null ;
@@ -113,8 +115,8 @@ export function textForMapboxFeature(feature: MapBoxFeature): string {
113
115
return pieces . join ( ", " ) ;
114
116
}
115
117
116
- export function textForMapboxResults ( results : MapBoxFeatureCollection ) : string {
117
- const feature = findBestFeature ( results ) ;
118
+ export function textForMapboxResults ( results : MapBoxFeatureCollection , relevantTypes ?: MapBoxFeatureType [ ] ) : string {
119
+ const feature = findBestFeature ( results , relevantTypes ) ;
118
120
return feature !== null ? textForMapboxFeature ( feature ) : "" ;
119
121
}
120
122
0 commit comments