Skip to content

Commit c2101ba

Browse files
DennisOSRMCopilotCopilot
authored
fix(geocoder): update implementation and adjust replace script (#444)
* fix(geocoder): update implementation and adjust replace script This PR updates the geocoder implementation, adjusts scripts/replace.js, and fixes geocoder tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 86c99aa commit c2101ba

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

scripts/replace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function applyReplacements(content, env) {
3535
// Leaflet uses LatLng
3636
else options = options.replace('38.8995,-77.0269', latlng)
3737
}
38-
if (NOMINATIM_ENDPOINT) options = options.replace('https://nominatim.openstreetmap.org/', NOMINATIM_ENDPOINT)
38+
if (NOMINATIM_ENDPOINT) options = options.replace(/https?:\/\/nominatim\.openstreetmap\.org\/?/g, function() { return NOMINATIM_ENDPOINT; })
3939

4040
return options
4141
}

src/geocoder.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ function parseCoords(query) {
5252
// Also bridges leaflet-control-geocoder's Promise API to the callback-based API
5353
// that leaflet-routing-machine's autocomplete expects.
5454
geocoder.coordPreserving = function(nominatimUrl) {
55-
var nominatim = L.Control.Geocoder.nominatim({serviceUrl: nominatimUrl});
55+
var nominatim;
56+
var normalizedNominatimUrl = typeof nominatimUrl === 'string' ? nominatimUrl.trim() : '';
57+
if (normalizedNominatimUrl.length > 0) {
58+
nominatim = L.Control.Geocoder.nominatim({serviceUrl: normalizedNominatimUrl});
59+
} else {
60+
// Preserve Leaflet-Control-Geocoder's default behavior when no URL provided
61+
nominatim = L.Control.Geocoder.nominatim();
62+
}
5663

5764
function withCallback(promise, cb, context) {
5865
return promise.then(function(results) {

test/geocoder.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ describe('geocoder.coordPreserving', () => {
2929
expect(results[0].center.lng).toBeCloseTo(-118.141254);
3030
expect(reverseMock).toHaveBeenCalled();
3131
});
32+
33+
test('invokes L.Control.Geocoder.nominatim() with no args when nominatimUrl omitted', () => {
34+
const reverseMock = jest.fn(() => Promise.resolve([]));
35+
const geocodeMock = jest.fn(() => Promise.resolve([]));
36+
const nominatimFactory = jest.fn(() => ({ reverse: reverseMock, geocode: geocodeMock }));
37+
38+
jest.doMock('leaflet', () => ({
39+
Control: { Geocoder: { nominatim: nominatimFactory } },
40+
CRS: { EPSG3857: { scale: () => 1 } },
41+
latLng: (lat, lng) => ({ lat: +lat, lng: +lng, toBounds: () => ({}) }),
42+
extend: Object.assign
43+
}));
44+
45+
const geocoder = require('../src/geocoder');
46+
const L = require('leaflet');
47+
48+
// Call without argument to ensure default nominatim factory is used
49+
geocoder.coordPreserving();
50+
expect(L.Control.Geocoder.nominatim).toHaveBeenCalled();
51+
expect(L.Control.Geocoder.nominatim.mock.calls[0].length).toBe(0);
52+
});
3253
});

0 commit comments

Comments
 (0)