18 lines
562 B
TypeScript
18 lines
562 B
TypeScript
// Normalize enum-like tokens in text: e.g. ADMIN -> Admin, NO_RISK -> No risk, MODERATE_PRIORITY -> Moderate priority
|
|
export const normalizeEnumLike = (input?: string): string => {
|
|
if (!input) return "";
|
|
|
|
const capitalize = (word: string, isFirst: boolean) =>
|
|
isFirst
|
|
? word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
: word.toLowerCase();
|
|
|
|
return input.replace(/\b[A-Z][A-Z0-9_]+\b/g, (token) =>
|
|
token
|
|
.split("_")
|
|
.filter(Boolean)
|
|
.map((part, idx) => capitalize(part, idx === 0))
|
|
.join(" ")
|
|
);
|
|
};
|