-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 |
||
{ id: 'short', name: 'Short Dots' }, | ||
{ id: 'base64', name: 'Base64 Decode'} | ||
]; | ||
|
||
_String.sampleInputs = [ | ||
'A Quick Brown Fox.', | ||
'STAY CALM!', | ||
'com.organizations.project.ClassName', | ||
'hostname.net', | ||
'SGVsbG8gd29ybGQ=' | ||
|
@@ -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 commentThe 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 commentThe 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:
The lodash functions strip punctuation (including underscores) and do not process all uppercase words properly:
I see that the lodash functions are used in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... |
||
}; | ||
|
||
_String.prototype._convert = function (val) { | ||
switch (this.param('transform')) { | ||
case 'lower': return String(val).toLowerCase(); | ||
case 'upper': return String(val).toUpperCase(); | ||
case 'title': return this._toTitleCase(val); | ||
case 'short': return _.shortenDottedString(val); | ||
case 'base64': return this._base64Decode(val); | ||
default: return _.asPrettyString(val); | ||
|
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!".