This commit is contained in:
Andras Schmelczer 2026-03-12 22:11:00 +00:00
parent 14a3555cf1
commit 7e92bf112e
34 changed files with 1214437 additions and 224 deletions

View file

@ -8,6 +8,15 @@ function looksLikePostcode(s: string) {
return POSTCODE_RE.test(s.trim());
}
/** Normalize a UK postcode: uppercase, strip spaces, insert canonical space before inward code. */
function normalizePostcode(s: string): string {
const stripped = s.replace(/\s+/g, '').toUpperCase();
if (stripped.length >= 5) {
return stripped.slice(0, -3) + ' ' + stripped.slice(-3);
}
return stripped;
}
export type SearchResult =
| { type: 'postcode'; label: string }
| { type: 'place'; name: string; slug: string; place_type: string; lat: number; lon: number; city?: string };
@ -35,7 +44,7 @@ export function useLocationSearch(mode?: string) {
}
if (!mode && looksLikePostcode(trimmed)) {
setResults([{ type: 'postcode', label: trimmed.toUpperCase() }]);
setResults([{ type: 'postcode', label: normalizePostcode(trimmed) }]);
setOpen(true);
return;
}
@ -97,7 +106,7 @@ export function useLocationSearch(mode?: string) {
if (activeIndex >= 0 && activeIndex < results.length) {
onSelect(results[activeIndex]);
} else if (looksLikePostcode(query)) {
onSelect({ type: 'postcode', label: query.trim().toUpperCase() });
onSelect({ type: 'postcode', label: normalizePostcode(query) });
}
} else if (e.key === 'Escape') {
setOpen(false);