Skip to content

Instantly share code, notes, and snippets.

@tinncdev
Created July 26, 2021 10:02
Show Gist options
  • Save tinncdev/ce9502e50a21ee5a36046e6d8abe9ccc to your computer and use it in GitHub Desktop.
Save tinncdev/ce9502e50a21ee5a36046e6d8abe9ccc to your computer and use it in GitHub Desktop.
A simple function to transform any string to snake case, skip all none alphanumberic characters
// A simple function to transform any string to snake case, skip all none alphanumberic characters
// Example: `NET WET. 0.09 g / 0.003 OZ.` will output `net_wet_0_09_g_0_003_oz`
const toSnakeCase = (text) => text.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('_');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment