Skip to content

Instantly share code, notes, and snippets.

@jpalomar
Last active June 10, 2021 23:50
Show Gist options
  • Save jpalomar/2bcd6093965ec15ec587a80bab13dd46 to your computer and use it in GitHub Desktop.
Save jpalomar/2bcd6093965ec15ec587a80bab13dd46 to your computer and use it in GitHub Desktop.
convert a string to SCREAMING_SNAKE_CASE
const toScreamingSnakeCase = (
str: string,
{ WORD_SEPARATORS, CAPITALS } = {
WORD_SEPARATORS: /[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]+/,
CAPITALS: /[A-Z\u00C0-\u00D6\u00D9-\u00DD]/g,
}
): string =>
str
.replace(CAPITALS, (match) => ' ' + (match.toUpperCase() || match))
.trim()
.split(WORD_SEPARATORS)
.join('_');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment