Nest all content into snippets
This commit is contained in:
30
snippets/js/s/to-pascal-case.md
Normal file
30
snippets/js/s/to-pascal-case.md
Normal file
@ -0,0 +1,30 @@
|
||||
---
|
||||
title: Pascalcase string
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [string,regexp]
|
||||
cover: camera-zoom
|
||||
dateModified: 2021-09-08T19:21:13+00:00
|
||||
---
|
||||
|
||||
Converts a string to pascal case.
|
||||
|
||||
- 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.toUpperCase()` and `String.prototype.toLowerCase()` to combine them, capitalizing the first letter of each word and lowercasing the rest.
|
||||
|
||||
```js
|
||||
const toPascalCase = 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.charAt(0).toUpperCase() + x.slice(1).toLowerCase())
|
||||
.join('');
|
||||
```
|
||||
|
||||
```js
|
||||
toPascalCase('some_database_field_name'); // 'SomeDatabaseFieldName'
|
||||
toPascalCase('Some label that needs to be pascalized');
|
||||
// 'SomeLabelThatNeedsToBePascalized'
|
||||
toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty'
|
||||
toPascalCase('some-mixed_string with spaces_underscores-and-hyphens');
|
||||
// 'SomeMixedStringWithSpacesUnderscoresAndHyphens'
|
||||
```
|
||||
Reference in New Issue
Block a user