implemented toPairs

main
Inga 🏳‍🌈 5 months ago
parent 7f0ca80e54
commit 2b66d40a5d
  1. 17
      src/routePlanner/hooks/useMarkersOnMap/getLayersForMarkers.ts
  2. 34
      src/shared/collections.test.ts
  3. 12
      src/shared/collections.ts
  4. 21
      src/shared/routes.ts

@ -1,4 +1,5 @@
import leaflet from 'leaflet';
import { toPairs } from '../../../shared/collections';
import type { Marker } from '../../types';
export type MarkerLayersStyle = {
@ -10,19 +11,9 @@ export const getLayersForMarkers = (
markerLayersStyle: MarkerLayersStyle,
) => [
// lines
...markers.flatMap((thisMarker, i) => {
const previousMarker = markers[i - 1];
if (!previousMarker) {
return [];
}
return [
leaflet.polyline([
previousMarker.coordinates,
thisMarker.coordinates,
]),
];
}),
...toPairs(markers).map(([a, b]) =>
leaflet.polyline([a.coordinates, b.coordinates]),
),
// circles and labels
...markers.map((marker) =>
leaflet

@ -1,5 +1,5 @@
import t from 'tap';
import { reorderElements } from './collections.js';
import { reorderElements, toPairs } from './collections.js';
void t.test('reorderElements', (t) => {
const checkReordering = (
@ -40,3 +40,35 @@ void t.test('reorderElements', (t) => {
checkReordering(4, 4, 'abcde');
t.end();
});
void t.test('toPairs', (t) => {
t.matchOnlyStrict(toPairs([]), []);
t.matchOnlyStrict(toPairs(['a']), []);
t.matchOnlyStrict(toPairs(['a', 'b']), [['a', 'b']]);
t.matchOnlyStrict(toPairs(['a', 'b', 'c']), [
['a', 'b'],
['b', 'c'],
]);
t.matchOnlyStrict(toPairs(['a', 'b', 'c', 'd']), [
['a', 'b'],
['b', 'c'],
['c', 'd'],
]);
t.matchOnlyStrict(toPairs([undefined, 'b', 'c']), [
[undefined, 'b'],
['b', 'c'],
]);
t.matchOnlyStrict(toPairs(['a', undefined, 'c']), [
['a', undefined],
[undefined, 'c'],
]);
t.matchOnlyStrict(toPairs(['a', 'b', undefined]), [
['a', 'b'],
['b', undefined],
]);
t.matchOnlyStrict(toPairs([undefined, undefined, undefined]), [
[undefined, undefined],
[undefined, undefined],
]);
t.end();
});

@ -22,3 +22,15 @@ export const reorderElements = <T>(
return collection;
};
export const toPairs = <T>(collection: T[]): [T, T][] =>
collection.flatMap((element, index, collection) => {
if (!index) {
return [];
}
const previousElement = collection[index - 1];
// We just checked that index of the previous element is inside array bounds
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return [[previousElement!, element]];
});

@ -1,19 +1,8 @@
import haversineDistance from 'haversine-distance';
import { toPairs } from './collections';
import { Waypoint } from './types';
export const getRouteLength = (points: Waypoint[]) => {
let result = 0;
for (let i = 0; i < points.length; i++) {
const currentPoint = points[i];
const previousPoint = points[i - 1];
if (!currentPoint || !previousPoint) {
continue;
}
result += haversineDistance(
previousPoint.coordinates,
currentPoint.coordinates,
);
}
return result;
};
export const getRouteLength = (points: Waypoint[]) =>
toPairs(points)
.map(([a, b]) => haversineDistance(a.coordinates, b.coordinates))
.reduce((sum, value) => sum + value, 0);

Loading…
Cancel
Save