diff --git a/snippets/toSnakeCase.md b/snippets/toSnakeCase.md new file mode 100644 index 00000000000..957fd3a78f3 --- /dev/null +++ b/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' +``` diff --git a/tag_database b/tag_database index deb4c89027a..e1c6987d5e1 100644 --- a/tag_database +++ b/tag_database @@ -117,6 +117,7 @@ toCamelCase:string toDecimalMark:utility toEnglishDate:date toOrdinalSuffix:utility +toSnakeCase:string truncateString:string truthCheckCollection:object union:array