SPB Git forge
38commits 1branches 0releases
338.7 MBsize
maindefault branch
4 h agolast push
HTML 53.9% TypeScript 44.5% JavaScript 0.6% SQL 0.5%
1.2 KB · 31 lines typescript
Raw Blame History
1import { cache } from "react";2import { api } from "@/lib/api";3import { routes } from "@/lib/routes";45/** iso2 → canonical country slug (from /countries?all=1), memoised per request. */6export const countrySlugMap = cache(async (): Promise<Map<string, string>> => {7  const r = await api.countries({ all: 1 });8  const m = new Map<string, string>();9  for (const c of r.items) m.set(c.iso2.toUpperCase(), c.slug);10  return m;11});1213/** Fallback slug when the registry is unreachable: ASCII-folded, hyphenated name (matches the API's slugs for most countries). */14export function slugifyCountryName(name: string | null | undefined, iso2: string): string {15  if (!name) return iso2.toLowerCase();16  const folded = name17    .normalize("NFD")18    .replace(/[̀-ͯ]/g, "")19    .toLowerCase()20    .replace(/[^a-z0-9]+/g, "-")21    .replace(/(^-|-$)/g, "");22  return folded || iso2.toLowerCase();23}2425/** Href of the country page for an iso2 (canonical slug when known). */26export async function countryHref(iso2: string | null | undefined, name?: string | null): Promise<string | null> {27  if (!iso2) return null;28  const m = await countrySlugMap();29  return routes.country(m.get(iso2.toUpperCase()) ?? slugifyCountryName(name, iso2));30}31