-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Add a title case string formatter. #6413
Conversation
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(); }); |
There was a problem hiding this comment.
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(' ') ?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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' }, |
There was a problem hiding this comment.
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
var string = new StringFormat({ | ||
transform: 'title' | ||
}); | ||
expect(string.convert('PLEASE DO NOT SHOUT')).to.be('Please Do Not Shout'); |
There was a problem hiding this comment.
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!".
This fixes issue #6379.