Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fix nulls leaking into geo urls #7433

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions src/components/views/location/LocationPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,25 @@ export function getGeoUri(position: GeolocationPosition): string {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const alt = (
position.coords.altitude !== undefined
isNumeric(position.coords.altitude)
? `,${position.coords.altitude}`
: ""
);
const acc = (
position.coords.accuracy !== undefined
isNumeric(position.coords.accuracy)
? `;u=${ position.coords.accuracy }`
: ""
);
return `geo:${lat},${lon}${alt}${acc}`;
}

function isNumeric(n: number | null | undefined) {
return (
n !== null &&
n !== undefined &&
!isNaN(n) &&
isFinite(n)
);
}
andybalaam marked this conversation as resolved.
Show resolved Hide resolved

export default LocationPicker;
16 changes: 16 additions & 0 deletions test/components/views/location/LocationPicker-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ describe("LocationPicker", () => {
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4");
});

it("Nulls in location are not shown in URI", () => {
const pos: GeolocationPosition = {
coords: {
latitude: 43.2,
longitude: 12.4,
altitude: null,
accuracy: null,
altitudeAccuracy: null,
heading: null,
speed: null,
},
timestamp: 12334,
};
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4");
});

it("Renders a URI with 3 coords", () => {
const pos: GeolocationPosition = {
coords: {
Expand Down