use strict null checks

This commit is contained in:
sbriat
2023-08-25 15:16:33 +02:00
parent effe51b9a2
commit f15e7d11b1
16 changed files with 91 additions and 165 deletions

View File

@@ -22,6 +22,7 @@ export class Geodesic implements GeodesicPort {
lat2,
lon2,
);
if (!azimuth || !distance) throw new Error('Azimuth not found');
return { azimuth, distance };
};
}

View File

@@ -72,11 +72,12 @@ export class GraphhopperGeorouter implements GeorouterPort {
.map((point) => [point.lat, point.lon].join('%2C'))
.join('&point='),
].join('');
const route = await lastValueFrom(
return await lastValueFrom(
this.httpService.get(url).pipe(
map((res) =>
res.data ? this.createRoute(res, path.type) : undefined,
),
map((response) => {
if (response.data) return this.createRoute(response, path.type);
throw new Error();
}),
catchError((error: AxiosError) => {
if (error.code == AxiosError.ERR_BAD_REQUEST) {
throw new RouteNotFoundException(
@@ -88,7 +89,6 @@ export class GraphhopperGeorouter implements GeorouterPort {
}),
),
);
return route;
}),
);
return routes;
@@ -156,12 +156,20 @@ export class GraphhopperGeorouter implements GeorouterPort {
const indices = this.getIndices(points, snappedWaypoints);
const times = this.getTimes(durations, indices);
const distances = this.getDistances(instructions, indices);
return indices.map((index) => ({
lon: points[index][1],
lat: points[index][0],
distance: distances.find((distance) => distance.index == index)?.distance,
duration: times.find((time) => time.index == index)?.duration,
}));
return indices.map((index) => {
const duration = times.find((time) => time.index == index);
if (!duration)
throw new Error(`Duration not found for waypoint #${index}`);
const distance = distances.find((distance) => distance.index == index);
if (!distance && instructions.length > 0)
throw new Error(`Distance not found for waypoint #${index}`);
return {
lon: points[index][1],
lat: points[index][0],
distance: distance?.distance,
duration: duration.duration,
};
});
};
private getIndices = (
@@ -182,7 +190,7 @@ export class GraphhopperGeorouter implements GeorouterPort {
index: number;
originIndex: number;
waypoint: number[];
nearest: number;
nearest?: number;
distance: number;
}
>{
@@ -209,7 +217,7 @@ export class GraphhopperGeorouter implements GeorouterPort {
}
}
for (const missedWaypoint of missedWaypoints) {
indices[missedWaypoint.originIndex] = missedWaypoint.nearest;
indices[missedWaypoint.originIndex] = missedWaypoint.nearest as number;
}
return indices;
};