Add toTitleCase snippet and update tag_database accordingly

This commit is contained in:
ccjmne
2018-10-19 03:49:34 +02:00
parent f53f5ccfd3
commit c49c77f80e
2 changed files with 23 additions and 0 deletions

22
snippets/toTitleCase.md Normal file
View File

@ -0,0 +1,22 @@
### toTitleCase
Converts a string to title case.
The algorithm featured here breaks down as follows:
1. Break the string into words, using a [Regular Expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
2. Capitalize each of them
3. Stich them back together, using the whitespace `' '` character as a separator
```js
const toTitleCase = 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
toTitleCase('some_database_field_name'); // 'Some Database Field Name'
toTitleCase('Some label that needs to be title-cased'); // 'Some Label That Needs To Be Title Cased'
toTitleCase('some-package-name'); // 'Some Package Name'
toTitleCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'Some Mixed String With Spaces Underscores And Hyphens'
```

View File

@ -301,6 +301,7 @@ tomorrow:date,intermediate
toOrdinalSuffix:utility,math,intermediate
toSafeInteger:math,beginner
toSnakeCase:string,regexp,intermediate
toTitleCase:string,regepx,intermediate
transform:object,array,intermediate
triggerEvent:browser,event,intermediate
truncateString:string,beginner