panzerdp/voca

Feature Suggestion: Title Case

alanqthomas opened this issue · 4 comments

Feature Suggestion

Convert a string to title case.

Examples

v.titleCase('hello world')
// => 'Hello World
v.titleCase('Hello world')
// => 'Hello World
v.titleCase('hello World')
// => 'Hello World

Implentation Suggestions

This SO question has many suggestions.

The top answer uses string.replace() with regex:

function toTitleCase(str) {
  return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

Not sure about the coverage of all cases.

Options

In formal title case, words like 'a', 'it', 'of', etc. are ignored (not capitalized). Consider this as an option.
v.titleCase(subject, allWords)

v.titleCase('this is a title of great proportions', true);
// => 'This Is A Title Of Great Proportions'
v.titleCase('this is a title of great proportions', false);
// => 'This is a Title of Great Proportions'

Hey @alanqthomas,
Interesting idea.

What do you think about the punctuation . , ? removal from the sentence? Probably it should be kept in the title case too.

Thanks!

@panzerdp I would think all punctuation should be left intact as well.

Sounds good. I will implement this function.

Implemented in version 1.2.0.
See v.titleCase() docs.

Thank you @alanqthomas for the suggestion.