Update toPascalCase.md
This commit is contained in:
committed by
GitHub
parent
0a203b83ed
commit
480a0e5824
@ -4,23 +4,17 @@ tags: string,regexp,intermediate
|
|||||||
firstSeen: 2021-09-08T19:21:13+00:00
|
firstSeen: 2021-09-08T19:21:13+00:00
|
||||||
---
|
---
|
||||||
|
|
||||||
Converts a string to pascalcase.
|
Converts a string to pascal case.
|
||||||
|
|
||||||
- Use `String.prototype.match()` to break the string into words using an appropriate regexp.
|
- Use `String.prototype.match()` to break the string into words using an appropriate regexp.
|
||||||
- Use `Array.prototype.map()`, `Array.prototype.slice()`, `Array.prototype.join()`, `String.prototype.toLowerCase()` and `String.prototype.toUpperCase()` to combine them, capitalizing the first letter of each one.
|
- Use `Array.prototype.map()`, `Array.prototype.slice()`, `Array.prototype.join()`, `String.prototype.toUpperCase()` and `String.prototype.toLowerCase()` to combine them, capitalizing the first letter of each word and lowercasing the rest.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toPascalCase = str => {
|
const toPascalCase = str =>
|
||||||
const 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)
|
||||||
str
|
.map(x => x.charAt(0).toUpperCase() + x.slice(1).toLowerCase())
|
||||||
.match(
|
.join('');
|
||||||
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
|
|
||||||
)
|
|
||||||
.map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
|
|
||||||
.join('');
|
|
||||||
return s;
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -30,4 +24,4 @@ toPascalCase('Some label that needs to be pascalized');
|
|||||||
toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty'
|
toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty'
|
||||||
toPascalCase('some-mixed_string with spaces_underscores-and-hyphens');
|
toPascalCase('some-mixed_string with spaces_underscores-and-hyphens');
|
||||||
// 'SomeMixedStringWithSpacesUnderscoresAndHyphens'
|
// 'SomeMixedStringWithSpacesUnderscoresAndHyphens'
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user