Skip to content

Commit 61d7c1e

Browse files
authored
feat(common): add a titlecase pipe (#13324)
closes #11436
1 parent bf93389 commit 61d7c1e

File tree

9 files changed

+130
-161
lines changed

9 files changed

+130
-161
lines changed

modules/@angular/common/src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export * from './location/index';
1515
export {NgLocalization} from './localization';
1616
export {CommonModule} from './common_module';
1717
export {NgClass, NgFor, NgIf, NgPlural, NgPluralCase, NgStyle, NgSwitch, NgSwitchCase, NgSwitchDefault, NgTemplateOutlet} from './directives/index';
18-
export {AsyncPipe, DatePipe, I18nPluralPipe, I18nSelectPipe, JsonPipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, SlicePipe, UpperCasePipe} from './pipes/index';
18+
export {AsyncPipe, DatePipe, I18nPluralPipe, I18nSelectPipe, JsonPipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, SlicePipe, UpperCasePipe, TitleCasePipe} from './pipes/index';
1919
export {VERSION} from './version';
2020
export {Version} from '@angular/core';
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {Pipe, PipeTransform} from '@angular/core';
10+
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
11+
12+
/**
13+
* Transforms text to lowercase.
14+
*
15+
* {@example core/pipes/ts/lowerupper_pipe/lowerupper_pipe_example.ts region='LowerUpperPipe' }
16+
*
17+
* @stable
18+
*/
19+
@Pipe({name: 'lowercase'})
20+
export class LowerCasePipe implements PipeTransform {
21+
transform(value: string): string {
22+
if (!value) return value;
23+
if (typeof value !== 'string') {
24+
throw new InvalidPipeArgumentError(LowerCasePipe, value);
25+
}
26+
return value.toLowerCase();
27+
}
28+
}
29+
30+
/**
31+
* Transforms text to titlecase.
32+
*
33+
* @stable
34+
*/
35+
@Pipe({name: 'titlecase'})
36+
export class TitleCasePipe implements PipeTransform {
37+
transform(value: string): string {
38+
if (!value) return value;
39+
if (typeof value !== 'string') {
40+
throw new InvalidPipeArgumentError(TitleCasePipe, value);
41+
}
42+
43+
return value[0].toUpperCase() + value.substr(1).toLowerCase();
44+
}
45+
}
46+
47+
/**
48+
* Transforms text to uppercase.
49+
*
50+
* @stable
51+
*/
52+
@Pipe({name: 'uppercase'})
53+
export class UpperCasePipe implements PipeTransform {
54+
transform(value: string): string {
55+
if (!value) return value;
56+
if (typeof value !== 'string') {
57+
throw new InvalidPipeArgumentError(UpperCasePipe, value);
58+
}
59+
return value.toUpperCase();
60+
}
61+
}

modules/@angular/common/src/pipes/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
* This module provides a set of common Pipes.
1313
*/
1414
import {AsyncPipe} from './async_pipe';
15+
import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from './case_conversion_pipes';
1516
import {DatePipe} from './date_pipe';
1617
import {I18nPluralPipe} from './i18n_plural_pipe';
1718
import {I18nSelectPipe} from './i18n_select_pipe';
1819
import {JsonPipe} from './json_pipe';
19-
import {LowerCasePipe} from './lowercase_pipe';
2020
import {CurrencyPipe, DecimalPipe, PercentPipe} from './number_pipe';
2121
import {SlicePipe} from './slice_pipe';
22-
import {UpperCasePipe} from './uppercase_pipe';
2322

2423
export {
2524
AsyncPipe,
@@ -32,9 +31,11 @@ export {
3231
LowerCasePipe,
3332
PercentPipe,
3433
SlicePipe,
34+
TitleCasePipe,
3535
UpperCasePipe
3636
};
3737

38+
3839
/**
3940
* A collection of Angular pipes that are likely to be used in each and every application.
4041
*/
@@ -46,6 +47,7 @@ export const COMMON_PIPES = [
4647
SlicePipe,
4748
DecimalPipe,
4849
PercentPipe,
50+
TitleCasePipe,
4951
CurrencyPipe,
5052
DatePipe,
5153
I18nPluralPipe,

modules/@angular/common/src/pipes/lowercase_pipe.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

modules/@angular/common/src/pipes/uppercase_pipe.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from '@angular/common';
10+
11+
export function main() {
12+
describe('LowerCasePipe', () => {
13+
let pipe: LowerCasePipe;
14+
15+
beforeEach(() => { pipe = new LowerCasePipe(); });
16+
17+
it('should return lowercase', () => { expect(pipe.transform('FOO')).toEqual('foo'); });
18+
19+
it('should lowercase when there is a new value', () => {
20+
expect(pipe.transform('FOO')).toEqual('foo');
21+
expect(pipe.transform('BAr')).toEqual('bar');
22+
});
23+
24+
it('should not support other objects',
25+
() => { expect(() => pipe.transform(<any>{})).toThrowError(); });
26+
});
27+
28+
describe('TitleCasePipe', () => {
29+
let pipe: TitleCasePipe;
30+
31+
beforeEach(() => { pipe = new TitleCasePipe(); });
32+
33+
it('should return titlecase', () => { expect(pipe.transform('foo')).toEqual('Foo'); });
34+
35+
it('should titlecase when there is a new value', () => {
36+
expect(pipe.transform('bar')).toEqual('Bar');
37+
expect(pipe.transform('foo')).toEqual('Foo');
38+
});
39+
40+
it('should not support other objects',
41+
() => { expect(() => pipe.transform(<any>{})).toThrowError(); });
42+
});
43+
44+
describe('UpperCasePipe', () => {
45+
let pipe: UpperCasePipe;
46+
47+
beforeEach(() => { pipe = new UpperCasePipe(); });
48+
49+
it('should return uppercase', () => { expect(pipe.transform('foo')).toEqual('FOO'); });
50+
51+
it('should uppercase when there is a new value', () => {
52+
expect(pipe.transform('foo')).toEqual('FOO');
53+
expect(pipe.transform('bar')).toEqual('BAR');
54+
});
55+
56+
it('should not support other objects',
57+
() => { expect(() => pipe.transform(<any>{})).toThrowError(); });
58+
});
59+
}

modules/@angular/common/test/pipes/lowercase_pipe_spec.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

modules/@angular/common/test/pipes/uppercase_pipe_spec.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

tools/public_api_guard/common/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ export declare class SlicePipe implements PipeTransform {
223223
transform(value: any, start: number, end?: number): any;
224224
}
225225

226+
/** @stable */
227+
export declare class TitleCasePipe implements PipeTransform {
228+
transform(value: string): string;
229+
}
230+
226231
/** @stable */
227232
export declare class UpperCasePipe implements PipeTransform {
228233
transform(value: string): string;

0 commit comments

Comments
 (0)