fix(common): capitalize first letter of all words in TitleCasePipe (#13511)
This commit is contained in:
parent
4568d5ddac
commit
a23fa94ca8
@ -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.
|
* Transforms text to titlecase.
|
||||||
*
|
*
|
||||||
@ -40,7 +51,7 @@ export class TitleCasePipe implements PipeTransform {
|
|||||||
throw new InvalidPipeArgumentError(TitleCasePipe, value);
|
throw new InvalidPipeArgumentError(TitleCasePipe, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return value[0].toUpperCase() + value.substr(1).toLowerCase();
|
return value.split(/\b/g).map(word => titleCaseWord(word)).join('');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,14 @@ export function main() {
|
|||||||
|
|
||||||
it('should return titlecase', () => { expect(pipe.transform('foo')).toEqual('Foo'); });
|
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', () => {
|
it('should titlecase when there is a new value', () => {
|
||||||
expect(pipe.transform('bar')).toEqual('Bar');
|
expect(pipe.transform('bar')).toEqual('Bar');
|
||||||
expect(pipe.transform('foo')).toEqual('Foo');
|
expect(pipe.transform('foo')).toEqual('Foo');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user