mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
client: add resolveLegMode helper for endpoint-aware leg modes
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { resolveLegMode } from './legMode';
|
||||
|
||||
const P = (o?: Partial<{ leg_transport_mode: string | null; incoming_leg_transport_mode: string | null }>) =>
|
||||
({ isPlace: true, ...o });
|
||||
const B = () => ({ isPlace: false });
|
||||
|
||||
describe('resolveLegMode', () => {
|
||||
it('origin place with outgoing mode wins', () => {
|
||||
expect(resolveLegMode(P({ leg_transport_mode: 'cycling' }), P(), 'walking')).toBe('cycling');
|
||||
});
|
||||
it('origin place, no override -> day default', () => {
|
||||
expect(resolveLegMode(P(), P(), 'driving')).toBe('driving');
|
||||
});
|
||||
it('non-place origin -> destination incoming override', () => {
|
||||
expect(resolveLegMode(B(), P({ incoming_leg_transport_mode: 'transit' }), 'walking')).toBe('transit');
|
||||
});
|
||||
it('non-place origin, no incoming override -> day default', () => {
|
||||
expect(resolveLegMode(B(), P(), 'walking')).toBe('walking');
|
||||
});
|
||||
it('no place endpoint at all -> day default', () => {
|
||||
expect(resolveLegMode(B(), B(), 'walking')).toBe('walking');
|
||||
});
|
||||
it('incoming is inert when origin is a place', () => {
|
||||
expect(resolveLegMode(P(), P({ incoming_leg_transport_mode: 'transit' }), 'walking')).toBe('walking');
|
||||
});
|
||||
it('undefined dest falls back to day default for a non-place origin', () => {
|
||||
expect(resolveLegMode(B(), undefined, 'walking')).toBe('walking');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
export type LegPoint = {
|
||||
isPlace: boolean;
|
||||
leg_transport_mode?: string | null;
|
||||
incoming_leg_transport_mode?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Travel mode of the leg from `origin` to `dest`. The mode is owned by the leg's
|
||||
* place endpoint: the origin's outgoing override wins when the origin is a place;
|
||||
* otherwise the destination's incoming override applies (only meaningful when the
|
||||
* origin is not a place - a booking arrival or the morning hotel); otherwise the
|
||||
* day default. The incoming value is inert whenever the origin is a place.
|
||||
*/
|
||||
export function resolveLegMode(
|
||||
origin: LegPoint,
|
||||
dest: LegPoint | undefined,
|
||||
dayDefault: string,
|
||||
): string {
|
||||
if (origin.isPlace) return origin.leg_transport_mode ?? dayDefault;
|
||||
if (dest?.isPlace) return dest.incoming_leg_transport_mode ?? dayDefault;
|
||||
return dayDefault;
|
||||
}
|
||||
Reference in New Issue
Block a user