export const escapeHtml = (input: string): string =>
  input
    .replaceAll("&", "&amp;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll('"', "&quot;")
    .replaceAll("'", "&#039;");

export const attr = (input: string | undefined): string => (input ? escapeHtml(input) : "");

export const titleCase = (input: string): string =>
  input
    .toLowerCase()
    .split(/[-_ ]+/u)
    .filter(Boolean)
    .map((part) => `${part[0]?.toUpperCase() ?? ""}${part.slice(1)}`)
    .join(" ");

export const disabled = (value: boolean): string => (value ? ' disabled aria-disabled="true"' : "");
