Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function applyReplacements(content, env) {
// Leaflet uses LatLng
else options = options.replace('38.8995,-77.0269', latlng)
}
if (NOMINATIM_ENDPOINT) options = options.replace('https://nominatim.openstreetmap.org/', NOMINATIM_ENDPOINT)
if (NOMINATIM_ENDPOINT) options = options.replace(/https?:\/\/nominatim\.openstreetmap\.org\/?/g, function() { return NOMINATIM_ENDPOINT; })

Comment on lines 37 to 39
return options
}
Expand Down
9 changes: 8 additions & 1 deletion src/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ function parseCoords(query) {
// Also bridges leaflet-control-geocoder's Promise API to the callback-based API
// that leaflet-routing-machine's autocomplete expects.
geocoder.coordPreserving = function(nominatimUrl) {
var nominatim = L.Control.Geocoder.nominatim({serviceUrl: nominatimUrl});
var nominatim;
var normalizedNominatimUrl = typeof nominatimUrl === 'string' ? nominatimUrl.trim() : '';
if (normalizedNominatimUrl.length > 0) {
nominatim = L.Control.Geocoder.nominatim({serviceUrl: normalizedNominatimUrl});
} else {
// Preserve Leaflet-Control-Geocoder's default behavior when no URL provided
nominatim = L.Control.Geocoder.nominatim();
}

function withCallback(promise, cb, context) {
return promise.then(function(results) {
Expand Down
21 changes: 21 additions & 0 deletions test/geocoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,25 @@ describe('geocoder.coordPreserving', () => {
expect(results[0].center.lng).toBeCloseTo(-118.141254);
expect(reverseMock).toHaveBeenCalled();
});

test('invokes L.Control.Geocoder.nominatim() with no args when nominatimUrl omitted', () => {
const reverseMock = jest.fn(() => Promise.resolve([]));
const geocodeMock = jest.fn(() => Promise.resolve([]));
const nominatimFactory = jest.fn(() => ({ reverse: reverseMock, geocode: geocodeMock }));

jest.doMock('leaflet', () => ({
Control: { Geocoder: { nominatim: nominatimFactory } },
CRS: { EPSG3857: { scale: () => 1 } },
latLng: (lat, lng) => ({ lat: +lat, lng: +lng, toBounds: () => ({}) }),
extend: Object.assign
}));

const geocoder = require('../src/geocoder');
const L = require('leaflet');

// Call without argument to ensure default nominatim factory is used
geocoder.coordPreserving();
expect(L.Control.Geocoder.nominatim).toHaveBeenCalled();
expect(L.Control.Geocoder.nominatim.mock.calls[0].length).toBe(0);
});
});
Loading