Created
July 26, 2021 10:02
-
-
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
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 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