implemented toPairs

main
Inga 🏳‍🌈 6 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 leaflet from 'leaflet';
import { toPairs } from '../../../shared/collections';
import type { Marker } from '../../types'; import type { Marker } from '../../types';
export type MarkerLayersStyle = { export type MarkerLayersStyle = {
@ -10,19 +11,9 @@ export const getLayersForMarkers = (
markerLayersStyle: MarkerLayersStyle, markerLayersStyle: MarkerLayersStyle,
) => [ ) => [
// lines // lines
...markers.flatMap((thisMarker, i) => { ...toPairs(markers).map(([a, b]) =>
const previousMarker = markers[i - 1]; leaflet.polyline([a.coordinates, b.coordinates]),
if (!previousMarker) { ),
return [];
}
return [
leaflet.polyline([
previousMarker.coordinates,
thisMarker.coordinates,
]),
];
}),
// circles and labels // circles and labels
...markers.map((marker) => ...markers.map((marker) =>
leaflet leaflet

@ -1,5 +1,5 @@
import t from 'tap'; import t from 'tap';
import { reorderElements } from './collections.js'; import { reorderElements, toPairs } from './collections.js';
void t.test('reorderElements', (t) => { void t.test('reorderElements', (t) => {
const checkReordering = ( const checkReordering = (
@ -40,3 +40,35 @@ void t.test('reorderElements', (t) => {
checkReordering(4, 4, 'abcde'); checkReordering(4, 4, 'abcde');
t.end(); 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; 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 haversineDistance from 'haversine-distance';
import { toPairs } from './collections';
import { Waypoint } from './types'; import { Waypoint } from './types';
export const getRouteLength = (points: Waypoint[]) => { export const getRouteLength = (points: Waypoint[]) =>
let result = 0; toPairs(points)
for (let i = 0; i < points.length; i++) { .map(([a, b]) => haversineDistance(a.coordinates, b.coordinates))
const currentPoint = points[i]; .reduce((sum, value) => sum + value, 0);
const previousPoint = points[i - 1];
if (!currentPoint || !previousPoint) {
continue;
}
result += haversineDistance(
previousPoint.coordinates,
currentPoint.coordinates,
);
}
return result;
};

Loading…
Cancel
Save