update toCamelCase.md
This commit is contained in:
@ -5,8 +5,15 @@ Converts a string to camelcase.
|
|||||||
Use `replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
|
Use `replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toCamelCase = str =>
|
const toCamelCase = str => {
|
||||||
str.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase());
|
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 arr = str.match(regex);
|
||||||
|
arr = arr.map(x =>{
|
||||||
|
return x.slice(0,1).toUpperCase() + x.slice(1).toLowerCase();
|
||||||
|
});
|
||||||
|
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'
|
||||||
|
|||||||
@ -5,13 +5,14 @@ Converts a string to snakecase.
|
|||||||
Use `replace()` to add underscores before capital letters, convert `toLowerCase()`, then `replace()` hyphens and spaces with underscores.
|
Use `replace()` to add underscores before capital letters, convert `toLowerCase()`, then `replace()` hyphens and spaces with underscores.
|
||||||
|
|
||||||
```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;
|
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 arr = str.match(regex);
|
let arr = str.match(regex);
|
||||||
arr = arr.forEach(x =>{
|
arr = arr.map(x =>{
|
||||||
return s.toLowerCase();
|
return x.toLowerCase();
|
||||||
});
|
});
|
||||||
return arr.join('_')
|
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'
|
||||||
|
|||||||
Reference in New Issue
Block a user