Skip to content

Commit

Permalink
Merge pull request #321 from Chalarangelo/Chalarangelo-snakeCase
Browse files Browse the repository at this point in the history
[FEATURE] Add toSnakeCase
  • Loading branch information
Chalarangelo committed Dec 23, 2017
2 parents b192110 + 48e6591 commit 58cce7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions snippets/toSnakeCase.md
@@ -0,0 +1,14 @@
### toSnakeCase

Converts a string to snakecase.

Use `replace()` to add underscores before capital letters, convert `toLowerCase()`, then `replace()` hyphens and spaces with underscores.

```js
const toSnakeCase = str =>
str.replace(/(\w)([A-Z])/g, '$1_$2').replace(/[\s-_]+/g, '_').toLowerCase();
// toSnakeCase("camelCase") -> 'camel_case'
// toSnakeCase("some text") -> 'some_text'
// toSnakeCase("some-javascript-property") -> 'some_javascript_property'
// toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some_mixed_string_with_spaces_underscores_and_hyphens'
```
1 change: 1 addition & 0 deletions tag_database
Expand Up @@ -123,6 +123,7 @@ toDecimalMark:utility
toEnglishDate:date
toKebabCase:string
toOrdinalSuffix:utility
toSnakeCase:string
truncateString:string
truthCheckCollection:object
union:array
Expand Down

0 comments on commit 58cce7f

Please sign in to comment.