Last active
June 27, 2025 17:28
-
-
Save T99/fef726e34d8b3a9c1b76cd500c6f5b8d to your computer and use it in GitHub Desktop.
A TypeScript utility function for converting between different casing conventions.
This file contains hidden or 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
/** | |
* A union of the possible casing formats. | |
*/ | |
export type CasingFormat = | |
| "lower case" | |
| "UPPER CASE" | |
| "Title Case" | |
| "camelCase" | |
| "PascalCase" | |
| "snake_case" | |
| "SCREAMING_SNAKE_CASE" | |
| "camel_Snake_Case" | |
| "Pascal_Snake_Case"; | |
/** | |
* A regular expression used to split a string into words. | |
*/ | |
const TO_WORDS_REGEX: RegExp = /\S+?(?=\s|[A-Z]|$)/gu; | |
/** | |
* Returns an array of words from a string. | |
* | |
* @param words {string} The string to split into words. | |
* @return {string[]} An array of words extracted from the string. | |
*/ | |
const toWords = (words: string): string[] => words.match(TO_WORDS_REGEX) ?? []; | |
/** | |
* Returns the lower case version of a string. | |
* | |
* @param word {string} The string to convert to lower case. | |
* @return {string} The lower case version of the string. | |
*/ | |
const toLowerCase = (word: string): string => word.toLowerCase(); | |
/** | |
* Returns the upper case version of a string. | |
* | |
* @param word {string} The string to convert to upper case. | |
* @return {string} The upper case version of the string. | |
*/ | |
const toUpperCase = (word: string): string => word.toUpperCase(); | |
/** | |
* Returns the title case version of a string. | |
* | |
* @param word {string} The string to convert to title case. | |
* @return {string} The title case version of the string. | |
*/ | |
const toTitleCase = (word: string): string => { | |
const letters: string[] = word.split(""); | |
return letters.slice(0, 1).map(toUpperCase).join("") + | |
letters.slice(1).map(toLowerCase).join(""); | |
}; | |
/** | |
* Returns a modified version of the input string array, having removed all | |
* non-alphanumeric characters from each string. | |
* | |
* @param words {string[]} The strings to process. | |
* @return {string[]} An array of string with non-alphanumeric characters | |
* removed. | |
*/ | |
const stripNonAlphaNumeric = (...words: string[]): string[] => | |
words.map((word: string) => word.replace(/[^a-zA-Z0-9]/g, "")); | |
/** | |
* A mapping of casing formats to their respective formatting functions. | |
*/ | |
const CASING_FORMATTERS: Record<CasingFormat, (words: string[]) => string> = { | |
"lower case": (words: string[]): string => words.map(toLowerCase).join(" "), | |
"UPPER CASE": (words: string[]): string => words.map(toUpperCase).join(" "), | |
"Title Case": (words: string[]): string => words.map(toTitleCase).join(" "), | |
"camelCase": (words: string[]): string => [ | |
...stripNonAlphaNumeric(...words).slice(0, 1).map(toLowerCase), | |
...stripNonAlphaNumeric(...words).slice(1).map(toTitleCase) | |
].join(""), | |
"PascalCase": (words: string[]): string => | |
stripNonAlphaNumeric(...words).map(toTitleCase).join(""), | |
"snake_case": (words: string[]): string => | |
stripNonAlphaNumeric(...words).map(toLowerCase).join("_"), | |
"SCREAMING_SNAKE_CASE": (words: string[]): string => | |
stripNonAlphaNumeric(...words).map(toUpperCase).join("_"), | |
"camel_Snake_Case": (words: string[]): string => [ | |
...stripNonAlphaNumeric(...words).slice(0, 1).map(toLowerCase), | |
...stripNonAlphaNumeric(...words).slice(1).map(toTitleCase) | |
].join("_"), | |
"Pascal_Snake_Case": (words: string[]): string => | |
stripNonAlphaNumeric(...words).map(toTitleCase).join("_"), | |
}; | |
/** | |
* Converts a string to the specified casing format. | |
* | |
* @param input {string} The string to convert. | |
* @param casing {CasingFormat} The casing format to convert the string to. | |
* @return {string} The converted string in the specified casing format. | |
*/ | |
export const convertToCasing = (input: string, casing: CasingFormat): string => | |
CASING_FORMATTERS[casing](toWords(input)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment