Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a title case string formatter. #6413

Merged
merged 2 commits into from
Mar 18, 2016
Merged

Add a title case string formatter. #6413

merged 2 commits into from
Mar 18, 2016

Conversation

patrickfournier
Copy link
Contributor

This fixes issue #6379.

@elasticsearch-release
Copy link

Jenkins standing by to test this. If you aren't a maintainer, you can ignore this comment. Someone with commit access, please review this and clear it for Jenkins to run; then say 'jenkins, test it'.

@@ -55,10 +57,15 @@ export default function _StringProvider(Private) {
}
};

_String.prototype._toTitleCase = function (val) {
return val.replace(/\w\S*/g, txt => { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or words(val).map(capitalize).join(' ') ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex behaves more like what is expected from the (English) titlecase function:

A Quick Brown Fox.                      A Quick Brown Fox.
STAY CALM!                              Stay Calm!
Mean, variance and standard_deviation.  Mean, Variance And Standard_deviation.

The lodash functions strip punctuation (including underscores) and do not process all uppercase words properly:

A Quick Brown Fox.                      A Quick Brown Fox
STAY CALM!                              STAY CALM
Mean, variance and standard_deviation.  Mean Variance And Standard Deviation

I see that the lodash functions are used in ui/public/filters/label.js. Maybe the regex should be used there too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patrickfournier That's where I saw it and thought it should probably be the same. Good point...

@@ -36,12 +36,14 @@ export default function _StringProvider(Private) {
{ id: false, name: '- none -' },
{ id: 'lower', name: 'Lower Case' },
{ id: 'upper', name: 'Upper Case' },
{ id: 'title', name: 'Title Case' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a comment for the pull author, but rather for us. We need to make this list pluggable

@rashidkpc rashidkpc added the help wanted adoptme label Mar 4, 2016
var string = new StringFormat({
transform: 'title'
});
expect(string.convert('PLEASE DO NOT SHOUT')).to.be('Please Do Not Shout');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests that explain the other functionality of the regex, such as the fact that it maintains punctuation. I don't want someone to see this code later and say "Oh, there's a lodash function that does this!".

@rashidkpc rashidkpc self-assigned this Mar 18, 2016
@rashidkpc rashidkpc merged commit 79e868c into elastic:master Mar 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants