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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ui/public/stringify/__tests__/_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ describe('String Format', function () {
expect(string.convert('Zm9vYmFy')).to.be('foobar');
});

it('convert a string to title case', function () {
var StringFormat = fieldFormats.getType('string');
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!".

expect(string.convert('Mean, variance and standard_deviation.')).to.be('Mean, Variance And Standard_deviation.');
expect(string.convert('Stay CALM!')).to.be('Stay Calm!');
});

});
7 changes: 7 additions & 0 deletions src/ui/public/stringify/types/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

{ 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='
Expand All @@ -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...

};

_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);
Expand Down