Merge pull request #321 from Chalarangelo/Chalarangelo-snakeCase

[FEATURE] Add toSnakeCase
This commit is contained in:
Angelos Chalaris
2017-12-24 00:01:46 +02:00
committed by GitHub
2 changed files with 15 additions and 0 deletions

14
snippets/toSnakeCase.md Normal file
View File

@ -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'
```

View File

@ -123,6 +123,7 @@ toDecimalMark:utility
toEnglishDate:date
toKebabCase:string
toOrdinalSuffix:utility
toSnakeCase:string
truncateString:string
truthCheckCollection:object
union:array