From 29713a4b3ab7c2c130be697e9480f8c1ce73814a Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Tue, 21 Dec 2021 16:02:04 +0000 Subject: [PATCH 1/2] Fix nulls leaking into geo urls --- src/components/views/location/LocationPicker.tsx | 13 +++++++++++-- .../views/location/LocationPicker-test.tsx | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/components/views/location/LocationPicker.tsx b/src/components/views/location/LocationPicker.tsx index dc383d67a49..0a8e008592e 100644 --- a/src/components/views/location/LocationPicker.tsx +++ b/src/components/views/location/LocationPicker.tsx @@ -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) + ); +} + export default LocationPicker; diff --git a/test/components/views/location/LocationPicker-test.tsx b/test/components/views/location/LocationPicker-test.tsx index 0d58612da68..e28c8cea78e 100644 --- a/test/components/views/location/LocationPicker-test.tsx +++ b/test/components/views/location/LocationPicker-test.tsx @@ -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: { From 08e80e1c5eea5b127e1ff995cd3be9d14dc369fd Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Tue, 21 Dec 2021 16:50:12 +0000 Subject: [PATCH 2/2] Use Number.isFinite to check numeric values --- src/components/views/location/LocationPicker.tsx | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/components/views/location/LocationPicker.tsx b/src/components/views/location/LocationPicker.tsx index 0a8e008592e..b00a6678bfb 100644 --- a/src/components/views/location/LocationPicker.tsx +++ b/src/components/views/location/LocationPicker.tsx @@ -289,25 +289,16 @@ export function getGeoUri(position: GeolocationPosition): string { const lat = position.coords.latitude; const lon = position.coords.longitude; const alt = ( - isNumeric(position.coords.altitude) + Number.isFinite(position.coords.altitude) ? `,${position.coords.altitude}` : "" ); const acc = ( - isNumeric(position.coords.accuracy) + Number.isFinite(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) - ); -} - export default LocationPicker;