Unit tests for parseGeoUri (#7395)
This commit is contained in:
parent
8f3ea97729
commit
9a8265429c
2 changed files with 184 additions and 22 deletions
|
@ -45,7 +45,7 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
|
|||
const loc = content[LOCATION_EVENT_TYPE.name];
|
||||
const uri = loc ? loc.uri : content['geo_uri'];
|
||||
|
||||
this.coords = this.parseGeoUri(uri);
|
||||
this.coords = parseGeoUri(uri);
|
||||
this.state = {
|
||||
error: undefined,
|
||||
};
|
||||
|
@ -53,27 +53,6 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
|
|||
this.description = loc?.description ?? content['body'];
|
||||
}
|
||||
|
||||
private parseGeoUri = (uri: string): GeolocationCoordinates => {
|
||||
const m = uri.match(/^\s*geo:(.*?)\s*$/);
|
||||
if (!m) return;
|
||||
const parts = m[1].split(';');
|
||||
const coords = parts[0].split(',');
|
||||
let uncertainty: number;
|
||||
for (const param of parts.slice(1)) {
|
||||
const m = param.match(/u=(.*)/);
|
||||
if (m) uncertainty = parseFloat(m[1]);
|
||||
}
|
||||
return {
|
||||
latitude: parseFloat(coords[0]),
|
||||
longitude: parseFloat(coords[1]),
|
||||
altitude: parseFloat(coords[2]),
|
||||
accuracy: uncertainty,
|
||||
altitudeAccuracy: undefined,
|
||||
heading: undefined,
|
||||
speed: undefined,
|
||||
};
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
const config = SdkConfig.get();
|
||||
const coordinates = new maplibregl.LngLat(this.coords.longitude, this.coords.latitude);
|
||||
|
@ -116,3 +95,24 @@ export default class MLocationBody extends React.Component<IBodyProps, IState> {
|
|||
</div>;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseGeoUri(uri: string): GeolocationCoordinates {
|
||||
const m = uri.match(/^\s*geo:(.*?)\s*$/);
|
||||
if (!m) return;
|
||||
const parts = m[1].split(';');
|
||||
const coords = parts[0].split(',');
|
||||
let uncertainty: number;
|
||||
for (const param of parts.slice(1)) {
|
||||
const m = param.match(/u=(.*)/);
|
||||
if (m) uncertainty = parseFloat(m[1]);
|
||||
}
|
||||
return {
|
||||
latitude: parseFloat(coords[0]),
|
||||
longitude: parseFloat(coords[1]),
|
||||
altitude: parseFloat(coords[2]),
|
||||
accuracy: uncertainty,
|
||||
altitudeAccuracy: undefined,
|
||||
heading: undefined,
|
||||
speed: undefined,
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue