diff --git a/src/lib/geo.test.ts b/src/lib/geo.test.ts index 7c3d2a3..c3cda78 100644 --- a/src/lib/geo.test.ts +++ b/src/lib/geo.test.ts @@ -35,6 +35,17 @@ describe("driverLabelFor", () => { expect(driverLabelFor(sorted, sorted[29].id)).toBe("4"); }); + it("clamps at '9' for the 35th driver and beyond (no '0' wrap)", () => { + const drivers = Array.from({ length: 40 }, (_, i) => ({ + id: `driver-${String(i).padStart(2, "0")}`, + })); + const sorted = [...drivers].sort((a, b) => a.id.localeCompare(b.id)); + // Sorted: driver-00 (A) ... driver-34 (9), then driver-35 clamps to "9". + expect(driverLabelFor(sorted, sorted[34].id)).toBe("9"); + expect(driverLabelFor(sorted, sorted[35].id)).toBe("9"); + expect(driverLabelFor(sorted, sorted[39].id)).toBe("9"); + }); + it("returns '?' for an unknown driver id", () => { const drivers = [{ id: "u-1" }]; expect(driverLabelFor(drivers, "u-missing")).toBe("?"); diff --git a/src/lib/geo.ts b/src/lib/geo.ts index f672970..4a3336f 100644 --- a/src/lib/geo.ts +++ b/src/lib/geo.ts @@ -237,9 +237,10 @@ export function colorForStop(args: { * ========================================================================= */ /** - * Returns the driver label for a given driver id within an alphabetically- + * Returns the driver label for a given driver id within an lexically- * sorted driver list. Position 0 → "A", 1 → "B", ..., 25 → "Z", 26 → "1", - * 27 → "2", ... Returns "?" if the id isn't in the list. + * 27 → "2", ..., 34 → "9", 35 → "9" (clamped). Returns "?" if the id + * isn't in the list. * * The caller MUST pass a list already sorted by `id` ascending — typically * the `driversWithItems` array, which `page.tsx` already sorts by name via @@ -254,9 +255,12 @@ export function driverLabelFor( const idx = sortedDrivers.findIndex((d) => d.id === driverId); if (idx === -1) return "?"; if (idx < 26) return String.fromCharCode("A".charCodeAt(0) + idx); - // After Z, fall back to 1..9 (and then loop — at >35 drivers we'll - // reuse digits, which is a graceful degradation rather than a crash). - return String((idx - 25) % 10); + // After Z, fall back to 1..9 and clamp at 9 — the glyph "0" would + // read as "unknown driver" next to the existing "?" fallback, and + // wrapping back to "1" would alias position 26's driver. Clamping + // makes it visually obvious you have >35 drivers without silently + // mislabeling anyone. + return String(Math.min(idx - 25, 9)); } /**