Files
TREK/client/src/components/Map/reservationName.ts
T
Maurice c435033343 refactor: one endpoint-name cleaner for both map renderers
The scan that replaced the parenthetical-stripping regex went into the Leaflet
overlay and its Mapbox GL counterpart as two byte-identical copies, which is
what the duplication gate caught. trackColors.ts next door exists for exactly
this reason - both renderers read a track's colour through one function so it
cannot come out different on one map than the other - so the cleaner follows it.

Behaviour is unchanged. The test brute-forces the new function against the regex
it replaced over all 55,987 names up to six characters of "ab() \t", and pins the
32k-space input that used to take a second.
2026-08-25 22:08:32 +02:00

32 lines
1.3 KiB
TypeScript

/**
* "Wien Hbf (Vienna)" → "Wien Hbf" — the endpoint name as a map label wants it,
* without the parenthetical a booking confirmation likes to append. Both map
* renderers go through here so an endpoint never reads one way on the Leaflet
* map and another on the GL one.
*
* Written as a scan rather than the /\s*\([^)]*\)/g it replaces. That pattern's
* leading \s* overlaps the engine's own restart-at-every-position scan, so a
* name that is a long run of spaces with no "(" after it backtracks
* quadratically (1.0s at 32k), and so does a run of "(" with no ")". An
* endpoint name is free text any trip member types and every other member's
* browser renders, so that is reachable across users.
*
* Matches the regex exactly: the whitespace directly before a "(" that has a
* ")" after it goes too, and an unclosed "(" is left alone.
*/
export function cleanEndpointName(name: string): string {
let out = ''
let cursor = 0
for (;;) {
const open = name.indexOf('(', cursor)
if (open === -1) break
const close = name.indexOf(')', open + 1)
if (close === -1) break
let start = open
while (start > cursor && /\s/.test(name[start - 1])) start--
out += name.slice(cursor, start)
cursor = close + 1
}
return (out + name.slice(cursor)).trim()
}