Skip to content

Case Converter — TypeScript source

Convert text between UPPER, lower, Title, Sentence, camelCase, PascalCase, snake_case, kebab-case, CONSTANT_CASE and slug. Live, one-click copy.

This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

// Text case transformations. Pure + deterministic - the unit-test surface for
// the Case Converter tool.

export function words(s: string): string[] {
  return s
    .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
    .split(/[\s_\-.]+/)
    .map((w) => w.replace(/[^a-zA-Z0-9]/g, ''))
    .filter(Boolean);
}

export function cap(s: string): string {
  return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
}

export const caseTransforms = {
  upper: (s: string) => s.toUpperCase(),
  lower: (s: string) => s.toLowerCase(),
  title: (s: string) => s.replace(/\w\S*/g, cap),
  sentence: (s: string) => {
    const t = s.toLowerCase().trim();
    return t ? t.charAt(0).toUpperCase() + t.slice(1) : '';
  },
  camel: (s: string) => words(s).map((w, i) => (i === 0 ? w.toLowerCase() : cap(w))).join(''),
  pascal: (s: string) => words(s).map(cap).join(''),
  snake: (s: string) => words(s).join('_').toLowerCase(),
  kebab: (s: string) => words(s).join('-').toLowerCase(),
  constant: (s: string) => words(s).join('_').toUpperCase(),
  slug: (s: string) => words(s).join('-').toLowerCase(),
} as const;

export type CaseId = keyof typeof caseTransforms;

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →