Last active
June 28, 2019 18:50
-
-
Save dfkaye/c34e2c9918e216e6b9f0c2328228cca0 to your computer and use it in GitHub Desktop.
headline.js - String#toTitleCase() - Capitalize Each Word In A String (Includes Unicode Support)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 8 March 2019 | |
// 17 March 2019 (Includes Unicode Support) | |
// 11 May 2019 - fix dreadful first-and-last mistake (Aditiyaa) | |
// 28 June 2019 - adds String.prototype.toTitleCase() method with test. | |
// Feels like I've done this before. | |
// Returns a headline (string) with each word capitalized. | |
// Not like this, OR LIKE THIS, But Like This. | |
function headline(title) { | |
var space = ' '; | |
return title | |
.normalize() | |
.toLowerCase() | |
.trim() | |
.split(space) | |
.map(function(w, i) { | |
i = 0; | |
while (w.charCodeAt(i) < 65 && i < w.length) { | |
// advance the index until we find a letter | |
i += 1; | |
} | |
return i < w.length | |
// found a letter: transform it, and return modified word | |
? w.substring(0, i) + w[i].toUpperCase() + w.substring(i + 1) | |
// no letters found: return original word | |
: w; | |
}) | |
.join(space); | |
} | |
/* test it out */ | |
var tests = [ | |
'ADITYAA RAMESHRRR', // Aditiyaa Rameshrrr | |
`"GOOD CODE DOCUMENTS ITSELF" AND OTHER HILARIOUS JOKES YOU SHOULDN’T TELL YOURSELF`, | |
"@#$!%%&*()", // ignores | |
'lowercase \u00E5ngstrom turned into an uppercase \u00E5ngstrom', // unicode | |
'\u00e7 \u00FE, \u021b, \u021d', // more unicode | |
'\u0075\u006e\u0069\u0063\u006f\u0064\u0065 \u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064' // unicode supported | |
]; | |
console.log(JSON.stringify(tests.map(function(title) { | |
return { title: title, headline: headline(title) }; | |
}), null, 2)); | |
/* | |
[ | |
{ | |
"title": "ADITYAA RAMESHRRR", | |
"headline": "Adityaa Rameshrrr" | |
}, | |
{ | |
"title": "\"GOOD CODE DOCUMENTS ITSELF\" AND OTHER HILARIOUS JOKES YOU SHOULDN’T TELL YOURSELF", | |
"headline": "\"Good Code Documents Itself\" And Other Hilarious Jokes You Shouldn’t Tell Yourself" | |
}, | |
{ | |
"title": "@#$!%%&*()", | |
"headline": "@#$!%%&*()" | |
}, | |
{ | |
"title": "lowercase ångstrom turned into an uppercase ångstrom", | |
"headline": "Lowercase Ångstrom Turned Into An Uppercase Ångstrom" | |
}, | |
{ | |
"title": "ç þ, ț, ȝ", | |
"headline": "Ç Þ, Ț, Ȝ" | |
}, | |
{ | |
"title": "unicode supported", | |
"headline": "Unicode Supported" | |
] | |
*/ | |
// You can add it to String.prototype as toTitleCase... | |
typeof String.prototype.toTitleCase == 'function' || (function() { | |
String.prototype.toTitleCase = function() { | |
return headline(this); | |
} | |
}()); | |
console.log(JSON.stringify(tests.map(function(title) { | |
return { title: title, titlecase: title.toTitleCase() }; | |
}), null, 2)); | |
/* | |
[ | |
{ | |
"title": "ADITYAA RAMESHRRR", | |
"titlecase": "Adityaa Rameshrrr" | |
}, | |
{ | |
"title": "\"GOOD CODE DOCUMENTS ITSELF\" AND OTHER HILARIOUS JOKES YOU SHOULDN’T TELL YOURSELF", | |
"titlecase": "\"Good Code Documents Itself\" And Other Hilarious Jokes You Shouldn’t Tell Yourself" | |
}, | |
{ | |
"title": "@#$!%%&*()", | |
"titlecase": "@#$!%%&*()" | |
}, | |
{ | |
"title": "lowercase ångstrom turned into an uppercase ångstrom", | |
"titlecase": "Lowercase Ångstrom Turned Into An Uppercase Ångstrom" | |
}, | |
{ | |
"title": "ç þ, ț, ȝ", | |
"titlecase": "Ç Þ, Ț, Ȝ" | |
}, | |
{ | |
"title": "unicode supported", | |
"titlecase": "Unicode Supported" | |
} | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment