Updated descriptions of string casing
This commit is contained in:
@ -2,18 +2,16 @@
|
|||||||
|
|
||||||
Converts a string to camelcase.
|
Converts a string to camelcase.
|
||||||
|
|
||||||
Use `replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
|
Break the string into words and combine them capitalizing the first letter of each word.
|
||||||
|
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toCamelCase = str => {
|
const toCamelCase = str => {
|
||||||
let regex = rx = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
|
let s = str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||||
let arr = str.match(regex);
|
.map(x => x.slice(0,1).toUpperCase() + x.slice(1).toLowerCase())
|
||||||
arr = arr.map(x =>{
|
.join('');
|
||||||
return x.slice(0,1).toUpperCase() + x.slice(1).toLowerCase();
|
return s.slice(0,1).toLowerCase() + s.slice(1)
|
||||||
});
|
}
|
||||||
str = arr.join('')
|
|
||||||
return str.slice(0,1).toLowerCase() + str.slice(1)
|
|
||||||
}
|
|
||||||
// toCamelCase("some_database_field_name") -> 'someDatabaseFieldName'
|
// toCamelCase("some_database_field_name") -> 'someDatabaseFieldName'
|
||||||
// toCamelCase("Some label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized'
|
// toCamelCase("Some label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized'
|
||||||
// toCamelCase("some-javascript-property") -> 'someJavascriptProperty'
|
// toCamelCase("some-javascript-property") -> 'someJavascriptProperty'
|
||||||
|
|||||||
@ -1,22 +1,15 @@
|
|||||||
### toKebabCase
|
### toKebabCase
|
||||||
|
|
||||||
Converts a string to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
|
Converts a string to kebab case.
|
||||||
Breaks the string into words.
|
|
||||||
A word is defined as following:-
|
|
||||||
-> Beginning with two or more capital letters, e.g. XML or FM
|
|
||||||
-> Begin with a capital letter followed by lower case letters with optional trailing numbers, e.g. Hello or Http2
|
|
||||||
-> Contain nothing but lower case letters with optional trailing numbers, e.g. hello or http2
|
|
||||||
-> Individual upper letters, e.g T.M.N.T
|
|
||||||
-> Groups of numbers, e.g. 555-555-5555
|
|
||||||
|
|
||||||
For more detailed explanation of this Regex [Visit this Site](https://regex101.com/r/bMCgAB/1)
|
Break the string into words and combine them using `-` as a separator.
|
||||||
|
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toKebabCase = str =>
|
const toKebabCase = str =>
|
||||||
str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||||
.map(x => x.toLowerCase())
|
.map(x => x.toLowerCase())
|
||||||
.join('-');
|
.join('-');
|
||||||
|
|
||||||
// toKebabCase("camelCase") -> 'camel-case'
|
// toKebabCase("camelCase") -> 'camel-case'
|
||||||
// toKebabCase("some text") -> 'some-text'
|
// toKebabCase("some text") -> 'some-text'
|
||||||
// toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens'
|
// toKebabCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some-mixed-string-with-spaces-underscores-and-hyphens'
|
||||||
|
|||||||
@ -1,20 +1,19 @@
|
|||||||
### toSnakeCase
|
### toSnakeCase
|
||||||
|
|
||||||
Converts a string to snakecase.
|
Converts a string to snake case.
|
||||||
|
|
||||||
Use `replace()` to add underscores before capital letters, convert `toLowerCase()`, then `replace()` hyphens and spaces with underscores.
|
Break the string into words and combine them using `_` as a separator.
|
||||||
|
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toSnakeCase = str =>{
|
const toSnakeCase = str =>{
|
||||||
let regex = rx = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g;
|
str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||||
let arr = str.match(regex);
|
.map(x => x.toLowerCase())
|
||||||
arr = arr.map(x =>{
|
.join('_');
|
||||||
return x.toLowerCase();
|
|
||||||
});
|
|
||||||
return arr.join('_')
|
|
||||||
}
|
|
||||||
// toSnakeCase("camelCase") -> 'camel_case'
|
// toSnakeCase("camelCase") -> 'camel_case'
|
||||||
// toSnakeCase("some text") -> 'some_text'
|
// toSnakeCase("some text") -> 'some_text'
|
||||||
// toSnakeCase("some-javascript-property") -> 'some_javascript_property'
|
// toSnakeCase("some-javascript-property") -> 'some_javascript_property'
|
||||||
// toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some_mixed_string_with_spaces_underscores_and_hyphens'
|
// toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some_mixed_string_with_spaces_underscores_and_hyphens'
|
||||||
|
// toKebabCase("AllThe-small Things") -> "all_the_smal_things"
|
||||||
|
// toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html"
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user