Skip to content
Open
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
4 changes: 1 addition & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ VITE_VALHALLA_URL=https://valhalla1.openstreetmap.de
VITE_NOMINATIM_URL=https://nominatim.openstreetmap.org
VITE_TILE_SERVER_URL="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
VITE_CENTER_COORDS="52.51831,13.393707"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated whitespace change, revert pls

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

# Possible values: auto, bicycle, pedestrian, car, truck, bus, motor_scooter, motorcycle
VITE_DEFAULT_COSTING_MODEL=bicycle
VITE_DEFAULT_COSTING_MODEL=bicycle
4 changes: 2 additions & 2 deletions src/components/settings-panel/settings-panel.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ describe('SettingsPanel', () => {
).toBeInTheDocument();
});

it('should render Reset button', () => {
it('should render Reset button', async () => {
renderWithQueryClient(<SettingsPanel />);
expect(
screen.getByRole('button', { name: /^Reset$/i })
await screen.findByRole('button', { name: /^Reset$/i })
).toBeInTheDocument();
});

Expand Down
66 changes: 66 additions & 0 deletions src/utils/nominatim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { parseGeocodeResponse } from './nominatim';
import type { NominationResponse } from '@/components/types';

describe('parseGeocodeResponse', () => {
let counter = 0;

beforeEach(() => {
counter = 0;
});

const makeResult = (name: string, lat?: string, lon?: string) =>
({
display_name: name,
lat: lat ?? `${++counter}.0000`,
lon: lon ?? `${++counter}.0000`,
osm_type: 'node',
osm_id: counter,
}) as unknown as NominationResponse;

it('returns all results when there are no duplicates', () => {
const results = [makeResult('Place A'), makeResult('Place B')];

const processed = parseGeocodeResponse(results);
expect(processed).toHaveLength(2);
expect(processed[0]!.title).toBe('Place A');
expect(processed[1]!.title).toBe('Place B');
});

it('removes entries that have identical coordinates and same name after diacritic normalization', () => {
const lat = `${++counter}.0000`;
const lon = `${++counter}.0000`;

const results = [
makeResult('Pláce C', lat, lon),
makeResult('Place C', lat, lon),
makeResult('PLÁCE C', lat, lon),
];

const processed = parseGeocodeResponse(results);
expect(processed).toHaveLength(1);
expect(processed[0]!.title).toBe('Pláce C');
});

it('keeps entries that have the same name but genuinely different coordinates', () => {
const results = [
makeResult('Place D'),
makeResult('Place D'),
makeResult('Place D'),
];

const processed = parseGeocodeResponse(results);
expect(processed).toHaveLength(3);
expect(processed[0]!.title).toBe('Place D');
expect(processed[1]!.title).toBe('Place D');
expect(processed[2]!.title).toBe('Place D');
});

it('handles a single non-array result without throwing', () => {
const result = makeResult('Place E');

const processed = parseGeocodeResponse(result);
expect(processed).toHaveLength(1);
expect(processed[0]!.title).toBe('Place E');
});
});
15 changes: 15 additions & 0 deletions src/utils/nominatim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const parseGeocodeResponse = (
}

const processedResults = [];
const seenKeys = new Set<string>();

for (const [index, result] of results.entries()) {
if (
'error' in result &&
Expand All @@ -48,6 +50,19 @@ export const parseGeocodeResponse = (
addressindex: index,
});
} else {
const normalizedTitle = result.display_name
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');
Comment on lines +53 to +56
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you leave some explanation what this represents?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually normalizes the dedup comparison, toLowercase makes it case insensitive , normalize('NFD') for characters like é and regex operation removes those . Eg: Île-de-France and Ile-de-France are same

const dedupeKey = result.boundingbox
? `${normalizedTitle}${result.boundingbox.join(',')}`
: `${normalizedTitle}${result.lat}${result.lon}`;

if (seenKeys.has(dedupeKey)) {
continue;
}
seenKeys.add(dedupeKey);
Comment on lines +61 to +64
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but there has to be some sort of set in JS (i.e. unique element container) which can do this in one line?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but seenKeys also does the same function :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha ok then follow up question: why you check for "has"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the key is already in the set, we skip it - if not, we add it. that's it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the whole point of a set: it only allows unique elements, so a seenKeys.add(dedupeKey) is all it should need for such a structure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops you are right, we dont want .has() here , we can use .add() to keep it simple

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 processedResults.push({
        title:
          result.display_name.length > 0
            ? result.display_name
            : lngLat?.toString() || '',
        description: `https://www.openstreetmap.org/${result.osm_type}/${result.osm_id}`,
        selected: false,
        addresslnglat: [parseFloat(result.lon), parseFloat(result.lat)],
        sourcelnglat:
          lngLat === undefined
            ? [parseFloat(result.lon), parseFloat(result.lat)]
            : lngLat,
        displaylnglat:
          lngLat !== undefined
            ? lngLat
            : [parseFloat(result.lon), parseFloat(result.lat)],
        key: index,
        addressindex: index,
      });

but wait, the job of .has() is to get out of the loop if it is already present and never execute the processedResults.push() , hope that makes sense (lines 66 - 84)


processedResults.push({
title:
result.display_name.length > 0
Expand Down
Loading