55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
/** Göteborg municipality postcodes used for delivery validation. */
|
|
const GOTHENBURG_POSTCODE_PREFIXES = ['41', '42', '43'] as const;
|
|
|
|
const GOTHENBURG_NAME_PATTERNS = [
|
|
'göteborg',
|
|
'gothenburg',
|
|
'goteborg',
|
|
'göteborgs stad',
|
|
'goteborgs stad',
|
|
] as const;
|
|
|
|
export interface DeliverableAddressFields {
|
|
city?: string;
|
|
municipality?: string;
|
|
postcode?: string;
|
|
countryCode?: string;
|
|
formatted?: string;
|
|
}
|
|
|
|
function normalize(value: string): string {
|
|
return value.trim().toLowerCase();
|
|
}
|
|
|
|
function matchesGothenburgName(value: string): boolean {
|
|
const normalized = normalize(value);
|
|
return GOTHENBURG_NAME_PATTERNS.some((pattern) => normalized.includes(pattern));
|
|
}
|
|
|
|
function hasGothenburgPostcode(postcode?: string): boolean {
|
|
if (!postcode) return false;
|
|
const digits = postcode.replace(/\s/g, '');
|
|
return GOTHENBURG_POSTCODE_PREFIXES.some((prefix) => digits.startsWith(prefix));
|
|
}
|
|
|
|
export function isGothenburgDeliveryAddress(fields: DeliverableAddressFields): boolean {
|
|
if (fields.countryCode && normalize(fields.countryCode) !== 'se') return false;
|
|
|
|
if (matchesGothenburgName(fields.city ?? '')) return true;
|
|
if (matchesGothenburgName(fields.municipality ?? '')) return true;
|
|
if (hasGothenburgPostcode(fields.postcode)) return true;
|
|
|
|
const formatted = normalize(fields.formatted ?? '');
|
|
if (
|
|
formatted.includes('göteborg') ||
|
|
formatted.includes('gothenburg') ||
|
|
formatted.includes('goteborg')
|
|
) {
|
|
return (
|
|
hasGothenburgPostcode(fields.postcode) ||
|
|
/\b41\d{3}\b|\b42\d{3}\b|\b43\d{3}\b/.test(formatted)
|
|
);
|
|
}
|
|
|
|
return false;
|
|
} |