41 lines
1022 B
TypeScript
41 lines
1022 B
TypeScript
const MAX_PUBLIC_AUDIT_SLUG_LENGTH = 120;
|
|
|
|
const transliterations: Record<string, string> = {
|
|
ä: "ae",
|
|
ö: "oe",
|
|
ü: "ue",
|
|
ß: "ss",
|
|
æ: "ae",
|
|
ø: "oe",
|
|
å: "a",
|
|
};
|
|
|
|
export const toPublicAuditSlug = (companyName: string, domain: string) => {
|
|
const input = `${companyName} ${domain}`.trim().toLowerCase();
|
|
const normalized = input
|
|
.replace(/[äöüßæøå]/g, (character) => transliterations[character] ?? character)
|
|
.normalize("NFKD")
|
|
.replace(/[\u0300-\u036f]/g, "")
|
|
.replace(/[^a-z0-9]+/g, "-")
|
|
.replace(/^-+|-+$/g, "")
|
|
.replace(/-{2,}/g, "-")
|
|
.slice(0, MAX_PUBLIC_AUDIT_SLUG_LENGTH)
|
|
.replace(/-+$/g, "");
|
|
|
|
return normalized.length > 0 ? normalized : "audit";
|
|
};
|
|
|
|
export const parsePublicAuditSlug = (slug: string) => {
|
|
const normalized = slug.trim().toLowerCase();
|
|
|
|
if (
|
|
normalized.length === 0 ||
|
|
normalized.length > MAX_PUBLIC_AUDIT_SLUG_LENGTH ||
|
|
!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(normalized)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return normalized;
|
|
};
|