Developer Documentation
TypeScript
Updated September 19, 2026

Convert Text Case in TypeScript

Convert words between sentence, title, camel, Pascal, snake, kebab, and constant case in TypeScript.

Case conversion looks simple until punctuation, acronyms, existing camelCase words, Unicode, and line breaks appear. Normalize text into tokens first, then render those tokens into the requested case.

Quick Start#

The following example uses the standard TypeScript API or a widely used package for this task.

Tokenize then render#

TypeScript
1function words(input: string): string[] {2  return input3    .replace(/([a-z0-])([A-Z])/g, "$1 $2")4    .trim()5    .split(/[^\p{L}\p{N}]+/u)6    .filter(Boolean)7    .map((word) => word.toLocaleLowerCase());8}910function capitalize(word: string): string {11  return word ? word[].toLocaleUpperCase() + word.slice() : word;12}1314export const toCamel = (input: string): string => {15  const parts = words(input);16  return parts.map((w, i) => i ===  ? w : capitalize(w)).join("");17};1819export const toSnake = (input: string): string => words(input).join("_");

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Typed case selection#

TypeScript
1type CaseKind = "camel" | "pascal" | "snake" | "kebab" | "constant";23function convertCase(input: string, kind: CaseKind): string {4  const parts = words(input);5  switch (kind) {6    case "camel": return parts.map((w, i) => i ? capitalize(w) : w).join("");7    case "pascal": return parts.map(capitalize).join("");8    case "snake": return parts.join("_");9    case "kebab": return parts.join("-");10    case "constant": return parts.join("_").toLocaleUpperCase();11  }12}

Important Notes#

Treat locale-sensitive casing deliberately. toLocaleUpperCase/toLocaleLowerCase can behave differently for some languages.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Text Case in other languages: