JSFiddle - React, Tailwind, and code Playground
by Abdul Ahmad
JavaScript
String.prototype.toTitleCase = function toTitleCase({ separator = ' ' } = {}) {
const finalWords = [];
this
.split(separator)
.forEach(w => {
const trimmed = w.trim();
if (!trimmed) return;
const titled = trimmed[0].toUpperCase() + trimmed.substr(1).toLowerCase();
finalWords.push(titled);
});
return finalWords.join(separator);
};
const titled = (' _ one _ two_three_ ').toTitleCase({ separator: '_' });
console.log(titled);