panzerdp / voca

The ultimate JavaScript string library
https://vocajs.pages.dev
MIT License
3.6k stars 137 forks source link

Feature Suggestion: Title Case #21

Closed alanqthomas closed 7 years ago

alanqthomas commented 7 years ago

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'
panzerdp commented 7 years ago

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!

alanqthomas commented 7 years ago

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

panzerdp commented 7 years ago

Sounds good. I will implement this function.

panzerdp commented 7 years ago

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

Thank you @alanqthomas for the suggestion.