From 144e4c61fcd0d0482b4314436fa8b9416a9bd757 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Fri, 17 Dec 2021 09:50:27 +0000 Subject: [PATCH] Avoid NaNs in parseGeoUri return (#7396) --- src/components/views/messages/MLocationBody.tsx | 17 +++++++++++++---- .../views/messages/MLocationBody-test.tsx | 14 +++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/components/views/messages/MLocationBody.tsx b/src/components/views/messages/MLocationBody.tsx index 197e751e15..d9131245d1 100644 --- a/src/components/views/messages/MLocationBody.tsx +++ b/src/components/views/messages/MLocationBody.tsx @@ -97,6 +97,15 @@ export default class MLocationBody extends React.Component { } export function parseGeoUri(uri: string): GeolocationCoordinates { + function parse(s: string): number { + const ret = parseFloat(s); + if (Number.isNaN(ret)) { + return undefined; + } else { + return ret; + } + } + const m = uri.match(/^\s*geo:(.*?)\s*$/); if (!m) return; const parts = m[1].split(';'); @@ -104,12 +113,12 @@ export function parseGeoUri(uri: string): GeolocationCoordinates { let uncertainty: number; for (const param of parts.slice(1)) { const m = param.match(/u=(.*)/); - if (m) uncertainty = parseFloat(m[1]); + if (m) uncertainty = parse(m[1]); } return { - latitude: parseFloat(coords[0]), - longitude: parseFloat(coords[1]), - altitude: parseFloat(coords[2]), + latitude: parse(coords[0]), + longitude: parse(coords[1]), + altitude: parse(coords[2]), accuracy: uncertainty, altitudeAccuracy: undefined, heading: undefined, diff --git a/test/components/views/messages/MLocationBody-test.tsx b/test/components/views/messages/MLocationBody-test.tsx index b38d3864b3..31281bec6a 100644 --- a/test/components/views/messages/MLocationBody-test.tsx +++ b/test/components/views/messages/MLocationBody-test.tsx @@ -52,7 +52,7 @@ describe("MLocationBody", () => { { latitude: 48.198634, longitude: 16.371648, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: 40, altitudeAccuracy: undefined, heading: undefined, @@ -66,7 +66,7 @@ describe("MLocationBody", () => { { latitude: 90, longitude: -22.43, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: undefined, altitudeAccuracy: undefined, heading: undefined, @@ -80,7 +80,7 @@ describe("MLocationBody", () => { { latitude: 90, longitude: 46, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: undefined, altitudeAccuracy: undefined, heading: undefined, @@ -94,7 +94,7 @@ describe("MLocationBody", () => { { latitude: 66, longitude: 30, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: 6.500, altitudeAccuracy: undefined, heading: undefined, @@ -108,7 +108,7 @@ describe("MLocationBody", () => { { latitude: 66.0, longitude: 30, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: 6.5, altitudeAccuracy: undefined, heading: undefined, @@ -122,7 +122,7 @@ describe("MLocationBody", () => { { latitude: 70, longitude: 20, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: undefined, altitudeAccuracy: undefined, heading: undefined, @@ -136,7 +136,7 @@ describe("MLocationBody", () => { { latitude: -7.5, longitude: 20, - altitude: NaN, // TODO: should be undefined + altitude: undefined, accuracy: undefined, altitudeAccuracy: undefined, heading: undefined,