update toCamelCase.md

This commit is contained in:
Rohit Tanwar
2017-12-24 20:20:06 +05:30
parent a51a2ef317
commit 25cb7036c7
2 changed files with 13 additions and 5 deletions

View File

@ -5,8 +5,15 @@ Converts a string to camelcase.
Use `replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
```js
const toCamelCase = str =>
str.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase());
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 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 label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized'
// toCamelCase("some-javascript-property") -> 'someJavascriptProperty'

View File

@ -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.
```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 arr = str.match(regex);
arr = arr.forEach(x =>{
return s.toLowerCase();
arr = arr.map(x =>{
return x.toLowerCase();
});
return arr.join('_')
}
// toSnakeCase("camelCase") -> 'camel_case'
// toSnakeCase("some text") -> 'some_text'
// toSnakeCase("some-javascript-property") -> 'some_javascript_property'