61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
export function normalizeStringEntries(list?: ReadonlyArray<unknown>) {
|
|
return (list ?? []).map((entry) => String(entry).trim()).filter(Boolean);
|
|
}
|
|
|
|
export function normalizeStringEntriesLower(list?: ReadonlyArray<unknown>) {
|
|
return normalizeStringEntries(list).map((entry) => entry.toLowerCase());
|
|
}
|
|
|
|
export function normalizeTrimmedStringList(value: unknown): string[] {
|
|
if (!Array.isArray(value)) {
|
|
return [];
|
|
}
|
|
return value.flatMap((entry) =>
|
|
typeof entry === "string" && entry.trim() ? [entry.trim()] : [],
|
|
);
|
|
}
|
|
|
|
export function normalizeSingleOrTrimmedStringList(value: unknown): string[] {
|
|
if (Array.isArray(value)) {
|
|
return normalizeTrimmedStringList(value);
|
|
}
|
|
if (typeof value === "string" && value.trim()) {
|
|
return [value.trim()];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
export function normalizeCsvOrLooseStringList(value: unknown): string[] {
|
|
if (Array.isArray(value)) {
|
|
return value.map((entry) => String(entry).trim()).filter(Boolean);
|
|
}
|
|
if (typeof value === "string") {
|
|
return value
|
|
.split(",")
|
|
.map((entry) => entry.trim())
|
|
.filter(Boolean);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
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, "");
|
|
}
|
|
|
|
export function normalizeAtHashSlug(raw?: string | null) {
|
|
const trimmed = raw?.trim().toLowerCase() ?? "";
|
|
if (!trimmed) {
|
|
return "";
|
|
}
|
|
const withoutPrefix = trimmed.replace(/^[@#]+/, "");
|
|
const dashed = withoutPrefix.replace(/[\s_]+/g, "-");
|
|
const cleaned = dashed.replace(/[^a-z0-9-]+/g, "-");
|
|
return cleaned.replace(/-{2,}/g, "-").replace(/^-+|-+$/g, "");
|
|
}
|