From 85d9882623dd1123a6f21e50f9537dce9e07bc21 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Fri, 22 Dec 2017 16:16:52 +0000 Subject: [PATCH] Travis build: 129 --- README.md | 18 ++++++++++++++++++ docs/index.html | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index d7b85ac23..6911e0d32 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ * [`reverseString`](#reversestring) * [`sortCharactersInString`](#sortcharactersinstring) * [`toCamelCase`](#tocamelcase) +* [`toSnakeCase`](#tosnakecase) * [`truncateString`](#truncatestring) * [`words`](#words) @@ -1911,6 +1912,23 @@ const toCamelCase = str => [⬆ back to top](#table-of-contents) +### 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(/[A-Z]/g, (match, p1, p2, offset) => '_' + match).toLowerCase().replace(/[\s-]+/g,'_'); +// 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' +``` + +[⬆ back to top](#table-of-contents) + ### truncateString Truncates a string up to a specified length. diff --git a/docs/index.html b/docs/index.html index 551066214..decde3adc 100644 --- a/docs/index.html +++ b/docs/index.html @@ -168,6 +168,7 @@ reverseString sortCharactersInString toCamelCase +toSnakeCase truncateString words @@ -1178,6 +1179,16 @@ Combine characters to get a string using join('').

// toCamelCase("some-javascript-property") -> 'someJavascriptProperty' // toCamelCase("some-mixed_string with spaces_underscores-and-hyphens") -> 'someMixedStringWithSpacesUnderscoresAndHyphens' +

toSnakeCase

+

Converts a string to snakecase.

+

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

+
const toSnakeCase = str =>
+  str.replace(/[A-Z]/g, (match, p1, p2, offset) => '_' + match).toLowerCase().replace(/[\s-]+/g,'_');
+// 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'
+

truncateString

Truncates a string up to a specified length.

Determine if the string's length is greater than num.