The code handles a number of edge cases, as outlined by Gruber:
- It knows about small words that should not be capitalized. Not all style guides use the same list of words — for example, many lowercase with, but I do not. The list of words is easily modified to suit your own taste/rules: “a an and as at but by en for if in of on or the to v[.]? via vs[.]?” (The only trickery here is that “v” and “vs” include optional dots, expressed in regex syntax.)
- The script assumes that words with capitalized letters other than the first character are already correctly capitalized. This means it will leave a word like “iTunes” alone, rather than mangling it into “ITunes” or, worse, “Itunes”.
- It also skips over any words with line dots; “example.com” and “del.icio.us” will remain lowercase.
- It has hard-coded hacks specifically to deal with odd cases I’ve run into, like “AT&T” and “Q&A”, both of which contain small words (at and a) which normally should be lowercase.
- The first and last word of the title are always capitalized, so input such as “Nothing to be afraid of” will be turned into “Nothing to Be Afraid Of”.
- A small word after a colon will be capitalized.
He goes on to provide a full list of edge cases that this script handles.
You would use the above code like so:
titleCaps("Nothing to Be Afraid of?") "Nothing to Be Afraid Of?" titleCaps("Q&A With Steve Jobs: 'That's What Happens In Technology'") "Q&A With Steve Jobs: 'That's What Happens in Technology'"
I hope this code will be useful to some – I suspect that it’ll be easy to plug into most blogging software (without having to worry about messing around with the server-side code), or even useful as some sort of bookmarklet.