mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-21 06:26:26 +00:00
1ad2e92142
The bulk of what SonarQube still had open on the reliability side, read site by
site rather than swept. Two clusters are worth calling out because they are
defects, not lint.
**Four regexes could be made to hang the process.** The worst is
`/.+@.+\..+/` in the forgot-password path: three unbounded `.+` that all
overlap, unanchored, on an endpoint that needs no login and whose schema is a
bare z.string() under a 100kb body cap. A thousand '@' characters had not
finished after 100 seconds — one request pins the event loop, and Node has one.
EMAIL_REGEX in auth.helpers is the same shape on register and profile update
(310ms for 8kb). The two tag strippers over an uploaded KML description and over
Commons metadata took 0.52s on a 16k run of '<'. Each rewrite was proven
equivalent by brute force against the original — millions of strings for the
coordinate patterns, 671,876 for the tag stripper — because a subtly different
regex on a parsing path is worse than the finding.
`<[^>]+>` could not be fixed in the pattern: `[^>]` matches `<` too, so the only
unambiguous form changes what happens to malformed markup. It is now a scan in
common/stripHtmlTags.ts, quirks included — `<>` is left alone, `<a<b>` is one
tag — with the equivalence pinned by a test that compares against the old regex
over every string up to length six.
**And roughly 500 places could only be reached with a mouse.** Clickable divs
became real buttons where the markup allowed (52 of them), took
role + tabIndex + Enter/Space where they had to stay divs, and modal backdrops
took role="presentation" — the honest role, since a backdrop is a mouse shortcut
for a Cancel control that is already in the tab order, and making it a button
plants a nameless full-screen tab stop around its own contents.
Alongside those: `.sort()` calls that now say the order they always had, `{n &&
<X/>}` guards that rendered a literal "0" for a zero-byte file and for a place at
latitude 0, an optimistic budget write that stored a boolean where the contract
and both transports use 0/1, and the Number statics.
sonar-project.properties documents the six that were deliberately left: two wiki
patterns with no overlapping quantifier pair whose "fix" would stop parsing
`[see [1]](url)`, four tar header fields bounded at 12-155 bytes by the format,
two djb2 hashes where the 32-bit wrap is the algorithm, two `| 0` that are also
coercing undefined to 0 on a path where the API sends no user_id, and the MCP
session maps Sonar reads as empty because they are filled from another module.
71 lines
3.0 KiB
TypeScript
71 lines
3.0 KiB
TypeScript
/**
|
||
* Formatting for a provider's weekday opening-hours lines.
|
||
*
|
||
* These arrive as display text — "Monday: 11:30 AM – 11:00 PM" from Google in
|
||
* whatever language was requested, "Monday: 09:00-18:00" from the OSM tag
|
||
* parser. They cannot be computed from (that is what `placeOpenState` and the
|
||
* structured periods are for), only rendered. Lifted out of PlaceInspector so
|
||
* the add-place detail column shows hours the same way the inspector does
|
||
* rather than growing a second, subtly different implementation.
|
||
*/
|
||
|
||
/** Rewrites a line between 12h and 24h clocks, leaving it alone when it already fits. */
|
||
export function convertHoursLine(line: string, timeFormat: string): string {
|
||
if (!line) return ''
|
||
const hasAmPm = /\d{1,2}:\d{2}\s*(AM|PM)/i.test(line)
|
||
|
||
if (timeFormat === '12h' && !hasAmPm) {
|
||
// 24h → 12h: "10:00" → "10:00 AM", "21:00" → "9:00 PM", "Uhr" entfernen.
|
||
// split/trimEnd rather than /\s*Uhr/g: that pattern walks the rest of the line
|
||
// from every space it passes, so a line with no "Uhr" in it costs a pass per
|
||
// space. Cutting on the word and trimming the piece in front of it is the
|
||
// same edit, once.
|
||
const parts = line.split('Uhr')
|
||
const withoutUhr = parts.map((part, i) => (i < parts.length - 1 ? part.trimEnd() : part)).join('')
|
||
return withoutUhr.replace(/(\d{1,2}):(\d{2})/g, (match, h, m) => {
|
||
const hour = Number.parseInt(h)
|
||
if (Number.isNaN(hour)) return match
|
||
const period = hour >= 12 ? 'PM' : 'AM'
|
||
const h12 = hour === 0 ? 12 : hour > 12 ? hour - 12 : hour
|
||
return `${h12}:${m} ${period}`
|
||
})
|
||
}
|
||
if (timeFormat !== '12h' && hasAmPm) {
|
||
// 12h → 24h: "10:00 AM" → "10:00", "9:00 PM" → "21:00"
|
||
return line.replace(/(\d{1,2}):(\d{2})\s*(AM|PM)/gi, (_, h, m, p) => {
|
||
let hour = Number.parseInt(h)
|
||
if (p.toUpperCase() === 'PM' && hour !== 12) hour += 12
|
||
if (p.toUpperCase() === 'AM' && hour === 12) hour = 0
|
||
return `${String(hour).padStart(2, '0')}:${m}`
|
||
})
|
||
}
|
||
return line
|
||
}
|
||
|
||
/**
|
||
* Splits "Monday: 09:00-18:00" into its day and its times.
|
||
*
|
||
* On the FIRST separator only, because the times contain colons of their own.
|
||
* The fullwidth colon is in there because that is what Google returns for
|
||
* Chinese and Japanese, and splitting on the ASCII one alone left those lines
|
||
* unsplit and the whole string in the day column.
|
||
*
|
||
* A line with no separator at all comes back with an empty day, which callers
|
||
* render across the full width rather than squeezing into a two-column row.
|
||
*/
|
||
export function splitHoursLine(line: string): [day: string, times: string] {
|
||
const at = line.search(/[::]/)
|
||
if (at < 0) return ['', line.trim()]
|
||
return [line.slice(0, at).trim(), line.slice(at + 1).trim()]
|
||
}
|
||
|
||
/**
|
||
* Whether a line says nothing about that day.
|
||
*
|
||
* `parseOpeningHours` writes "Monday: ?" for a day the OSM tag did not cover.
|
||
* Showing a bare question mark reads like a bug, so callers render a dash.
|
||
*/
|
||
export function isUnknownHoursLine(times: string): boolean {
|
||
return times.trim() === '?'
|
||
}
|