fix(common): capitalize first letter of all words in TitleCasePipe (#13511)

This commit is contained in:
Brandon 2016-12-16 16:24:26 -07:00 committed by Chuck Jazdzewski
parent 4568d5ddac
commit a23fa94ca8
2 changed files with 20 additions and 1 deletions

View File

@ -27,6 +27,17 @@ export class LowerCasePipe implements PipeTransform {
}
}
/**
* Helper method to transform a single word to titlecase.
*
* @stable
*/
function titleCaseWord(word: string) {
if (!word) return word;
return word[0].toUpperCase() + word.substr(1).toLowerCase();
}
/**
* Transforms text to titlecase.
*
@ -40,7 +51,7 @@ export class TitleCasePipe implements PipeTransform {
throw new InvalidPipeArgumentError(TitleCasePipe, value);
}
return value[0].toUpperCase() + value.substr(1).toLowerCase();
return value.split(/\b/g).map(word => titleCaseWord(word)).join('');
}
}

View File

@ -32,6 +32,14 @@ export function main() {
it('should return titlecase', () => { expect(pipe.transform('foo')).toEqual('Foo'); });
it('should return titlecase for subsequent words',
() => { expect(pipe.transform('one TWO Three fouR')).toEqual('One Two Three Four'); });
it('should support empty strings', () => { expect(pipe.transform('')).toEqual(''); });
it('should persist whitespace',
() => { expect(pipe.transform('one two')).toEqual('One Two'); });
it('should titlecase when there is a new value', () => {
expect(pipe.transform('bar')).toEqual('Bar');
expect(pipe.transform('foo')).toEqual('Foo');