Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created July 30, 2018 23:24
Show Gist options
  • Select an option

  • Save gladchinda/800726faa745ceabe49a82cb835c4ce0 to your computer and use it in GitHub Desktop.

Select an option

Save gladchinda/800726faa745ceabe49a82cb835c4ce0 to your computer and use it in GitHub Desktop.
const snakeCase = value => {
const regex = /[A-Z][^A-Z]+/g;
const withoutSpaces = value.trim().replace(/\s+/g, '_');
const caps = withoutSpaces.match(regex);
const splits = withoutSpaces.split(regex);
let finalString = splits.shift();
for (let i = 0; i < splits.length; i++) {
finalString += `${caps[i]}_${splits[i]}_`;
}
return finalString
.toLowerCase()
.replace(/_+/g, '_')
.replace(/^_?(.+?)_?$/, '$1');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment