Created
July 11, 2012 14:31
-
-
Save wengzilla/3090736 to your computer and use it in GitHub Desktop.
titlelize.js - wengzilla
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
String.prototype.titleize = function() { | |
var words = this.split(" ") | |
var word_count = words.length | |
for(var i = 0; i < word_count; i++) | |
if(i == 0 || !is_article(words[i])) | |
words[i] = words[i].capitalize() | |
return words.join(" "); | |
} | |
String.prototype.capitalize = function() { | |
var word = this; | |
if (word[0] && word.toUpperCase) | |
return word[0].toUpperCase() + word.slice(1); | |
} | |
function is_article(word) { | |
return (word == "a" || word == "an") ? true : false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment