smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.04k stars 273 forks source link

feat: add option to format results #256

Closed mrsimpson closed 3 years ago

mrsimpson commented 3 years ago

Motivation

Some search providers, especially OSM, return the address search results in a not very common order. E. g. the house number is returned in the first position of the label, which is very uncommon in German.

Changes done

This PR introduces a callback function which is executed in order to manipulate the search result label.

Caveats

As the query is currently not available in the result list, it could not be propagated to the callback function. Adding this would have increased the PR which I wanted to avoid.

How it looks like

Before

Provider OSM

without-callback

Afterwards

with callback injected

resultFormat: (result)=>{
          if(result.label.match(/^\d+,/)){
            // swap house no and street
            const components = result.label.split(',').map(s=>s.trim())
            const houseNo = components[0]
            components[0] = components[1]
            components[1] = houseNo
            return `${components.slice(0,2).join(' ')}, ${components.slice(2, components.length).join(',  ')}`
          }
          return result.label
          },
with-callback
mrsimpson commented 3 years ago

@smeijer any chance to get this merged? I tried to keep the diff as small as possible.

smeijer commented 3 years ago

Thanks! Looking great.

smeijer commented 3 years ago

published as 3.2.0, thanks again! :tada: