| 1 |
/** |
| 2 |
* Dash case to Title case. |
| 3 |
* |
| 4 |
* dashCaseToTitle('user-id') => "User Id" |
| 5 |
* dashCaseToTitle('wait-a-moment') => "Wait A Moment" |
| 6 |
* |
| 7 |
* https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript |
| 8 |
* |
| 9 |
* @param {string} str - dash-cased string. |
| 10 |
* @return {string} - new title-cased string. |
| 11 |
*/ |
| 12 |
export default function dashCaseToTitle(str) { |
| 13 |
if (typeof str !== 'string') { |
| 14 |
return str; |
| 15 |
} |
| 16 |
|
| 17 |
return str |
| 18 |
.split(/[.,/ \-_]/) |
| 19 |
.map((word) => |
| 20 |
word && word.length |
| 21 |
? word.replace(word[0], word[0].toUpperCase()) |
| 22 |
: word |
| 23 |
) |
| 24 |
.join(' '); |
| 25 |
} |
| 26 |
|