refactor: extract shared string normalization helpers
This commit is contained in:
17
src/shared/string-normalization.ts
Normal file
17
src/shared/string-normalization.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export function normalizeStringEntries(list?: Array<string | number>) {
|
||||
return (list ?? []).map((entry) => String(entry).trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
export function normalizeStringEntriesLower(list?: Array<string | number>) {
|
||||
return normalizeStringEntries(list).map((entry) => entry.toLowerCase());
|
||||
}
|
||||
|
||||
export function normalizeHyphenSlug(raw?: string | null) {
|
||||
const trimmed = raw?.trim().toLowerCase() ?? "";
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
const dashed = trimmed.replace(/\s+/g, "-");
|
||||
const cleaned = dashed.replace(/[^a-z0-9#@._+-]+/g, "-");
|
||||
return cleaned.replace(/-{2,}/g, "-").replace(/^[-.]+|[-.]+$/g, "");
|
||||
}
|
||||
Reference in New Issue
Block a user