Created
February 24, 2024 03:22
-
-
Save marcosleal-prd/102febc580ee463873c9ada4b84c240c to your computer and use it in GitHub Desktop.
String case conversion in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CONVERT_CASE_REGEXP = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g | |
export class StrCaseConversion { | |
static toCamelCase(str: string): string { | |
const result = str.match(CONVERT_CASE_REGEXP) | |
if (result) { | |
const s = result.map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()).join('') | |
return s.slice(0, 1).toLowerCase() + s.slice(1) | |
} | |
return str | |
} | |
static toPascalCase(str: string): string { | |
const result = str.match(CONVERT_CASE_REGEXP) | |
if (result) { | |
return result.map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()).join('') | |
} | |
return str | |
} | |
static toKebabCase(str: string): string { | |
const result = str.match(CONVERT_CASE_REGEXP) | |
if (result) { | |
return result.map(x => x.toLowerCase()).join('-') | |
} | |
return str | |
} | |
static toSnakeCase(str: string): string { | |
const result = str.match(CONVERT_CASE_REGEXP) | |
if (result) { | |
return result.map(x => x.toLowerCase()).join('_') | |
} | |
return str | |
} | |
static toTitleCase(str: string): string { | |
const result = str.match(CONVERT_CASE_REGEXP) | |
if (result) { | |
return result.map(x => x.slice(0, 1).toUpperCase() + x.slice(1)).join(' ') | |
} | |
return str | |
} | |
static toSentenceCase(str: string): string { | |
const result = str.match(CONVERT_CASE_REGEXP) | |
if (result) { | |
const s = result.join(' ') | |
return s.slice(0, 1).toUpperCase() + s.slice(1) | |
} | |
return str | |
} | |
} | |
// Examples | |
console.log('TO CAMEL') | |
console.log(StrCaseConversion.toCamelCase('some_database_field_name')) | |
console.log(StrCaseConversion.toCamelCase('Some label that needs to be camelized')) | |
console.log(StrCaseConversion.toCamelCase('some-javascript-property')) | |
console.log(StrCaseConversion.toCamelCase('some-mixed_string with spaces_underscores-and-hyphens')) | |
console.log('\n') | |
console.log('TO PASCAL') | |
console.log(StrCaseConversion.toPascalCase('some_database_field_name')) | |
console.log(StrCaseConversion.toPascalCase('Some label that needs to be pascalized')) | |
console.log(StrCaseConversion.toPascalCase('some-javascript-property')) | |
console.log(StrCaseConversion.toPascalCase('some-mixed_string with spaces_underscores-and-hyphens')) | |
console.log('\n') | |
console.log('TO KEBAB') | |
console.log(StrCaseConversion.toKebabCase('camelCase')) | |
console.log(StrCaseConversion.toKebabCase('some text')) | |
console.log(StrCaseConversion.toKebabCase('some-mixed_string With spaces_underscores-and-hyphens')) | |
console.log(StrCaseConversion.toKebabCase('AllThe-small Things')) | |
console.log(StrCaseConversion.toKebabCase('IAmEditingSomeXMLAndHTML')) | |
console.log('\n') | |
console.log('TO SNACK') | |
console.log(StrCaseConversion.toSnakeCase('camelCase')) | |
console.log(StrCaseConversion.toSnakeCase('some text')) | |
console.log(StrCaseConversion.toSnakeCase('some-mixed_string With spaces_underscores-and-hyphens')) | |
console.log(StrCaseConversion.toSnakeCase('AllThe-small Things')) | |
console.log(StrCaseConversion.toSnakeCase('IAmEditingSomeXMLAndHTML')) | |
console.log('\n') | |
console.log('TO TITLE') | |
console.log(StrCaseConversion.toTitleCase('some_database_field_name')) | |
console.log(StrCaseConversion.toTitleCase('Some label that needs to be title-cased')) | |
console.log(StrCaseConversion.toTitleCase('some-package-name')) | |
console.log(StrCaseConversion.toTitleCase('some-mixed_string with spaces_underscores-and-hyphens')) | |
console.log('\n') | |
console.log('TO SENTENCE') | |
console.log(StrCaseConversion.toSentenceCase('some_database_field_name')) | |
console.log(StrCaseConversion.toSentenceCase('Some label that needs to be title-cased')) | |
console.log(StrCaseConversion.toSentenceCase('some-package-name')) | |
console.log(StrCaseConversion.toSentenceCase('some-mixed_string with spaces_underscores-and-hyphens')) | |
console.log('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment