Skip to content

Commit

Permalink
feat(common): add a titlecase pipe (#13324)
Browse files Browse the repository at this point in the history
closes #11436
  • Loading branch information
vicb committed Dec 9, 2016
1 parent bf93389 commit 61d7c1e
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 161 deletions.
2 changes: 1 addition & 1 deletion modules/@angular/common/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export * from './location/index';
export {NgLocalization} from './localization';
export {CommonModule} from './common_module';
export {NgClass, NgFor, NgIf, NgPlural, NgPluralCase, NgStyle, NgSwitch, NgSwitchCase, NgSwitchDefault, NgTemplateOutlet} from './directives/index';
export {AsyncPipe, DatePipe, I18nPluralPipe, I18nSelectPipe, JsonPipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, SlicePipe, UpperCasePipe} from './pipes/index';
export {AsyncPipe, DatePipe, I18nPluralPipe, I18nSelectPipe, JsonPipe, LowerCasePipe, CurrencyPipe, DecimalPipe, PercentPipe, SlicePipe, UpperCasePipe, TitleCasePipe} from './pipes/index';
export {VERSION} from './version';
export {Version} from '@angular/core';
61 changes: 61 additions & 0 deletions modules/@angular/common/src/pipes/case_conversion_pipes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Pipe, PipeTransform} from '@angular/core';
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';

/**
* Transforms text to lowercase.
*
* {@example core/pipes/ts/lowerupper_pipe/lowerupper_pipe_example.ts region='LowerUpperPipe' }
*
* @stable
*/
@Pipe({name: 'lowercase'})
export class LowerCasePipe implements PipeTransform {
transform(value: string): string {
if (!value) return value;
if (typeof value !== 'string') {
throw new InvalidPipeArgumentError(LowerCasePipe, value);
}
return value.toLowerCase();
}
}

/**
* Transforms text to titlecase.
*
* @stable
*/
@Pipe({name: 'titlecase'})
export class TitleCasePipe implements PipeTransform {
transform(value: string): string {
if (!value) return value;
if (typeof value !== 'string') {
throw new InvalidPipeArgumentError(TitleCasePipe, value);
}

return value[0].toUpperCase() + value.substr(1).toLowerCase();
}
}

/**
* Transforms text to uppercase.
*
* @stable
*/
@Pipe({name: 'uppercase'})
export class UpperCasePipe implements PipeTransform {
transform(value: string): string {
if (!value) return value;
if (typeof value !== 'string') {
throw new InvalidPipeArgumentError(UpperCasePipe, value);
}
return value.toUpperCase();
}
}
6 changes: 4 additions & 2 deletions modules/@angular/common/src/pipes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
* This module provides a set of common Pipes.
*/
import {AsyncPipe} from './async_pipe';
import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from './case_conversion_pipes';
import {DatePipe} from './date_pipe';
import {I18nPluralPipe} from './i18n_plural_pipe';
import {I18nSelectPipe} from './i18n_select_pipe';
import {JsonPipe} from './json_pipe';
import {LowerCasePipe} from './lowercase_pipe';
import {CurrencyPipe, DecimalPipe, PercentPipe} from './number_pipe';
import {SlicePipe} from './slice_pipe';
import {UpperCasePipe} from './uppercase_pipe';

export {
AsyncPipe,
Expand All @@ -32,9 +31,11 @@ export {
LowerCasePipe,
PercentPipe,
SlicePipe,
TitleCasePipe,
UpperCasePipe
};


/**
* A collection of Angular pipes that are likely to be used in each and every application.
*/
Expand All @@ -46,6 +47,7 @@ export const COMMON_PIPES = [
SlicePipe,
DecimalPipe,
PercentPipe,
TitleCasePipe,
CurrencyPipe,
DatePipe,
I18nPluralPipe,
Expand Down
37 changes: 0 additions & 37 deletions modules/@angular/common/src/pipes/lowercase_pipe.ts

This file was deleted.

36 changes: 0 additions & 36 deletions modules/@angular/common/src/pipes/uppercase_pipe.ts

This file was deleted.

59 changes: 59 additions & 0 deletions modules/@angular/common/test/pipes/case_conversion_pipes_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {LowerCasePipe, TitleCasePipe, UpperCasePipe} from '@angular/common';

export function main() {
describe('LowerCasePipe', () => {
let pipe: LowerCasePipe;

beforeEach(() => { pipe = new LowerCasePipe(); });

it('should return lowercase', () => { expect(pipe.transform('FOO')).toEqual('foo'); });

it('should lowercase when there is a new value', () => {
expect(pipe.transform('FOO')).toEqual('foo');
expect(pipe.transform('BAr')).toEqual('bar');
});

it('should not support other objects',
() => { expect(() => pipe.transform(<any>{})).toThrowError(); });
});

describe('TitleCasePipe', () => {
let pipe: TitleCasePipe;

beforeEach(() => { pipe = new TitleCasePipe(); });

it('should return titlecase', () => { expect(pipe.transform('foo')).toEqual('Foo'); });

it('should titlecase when there is a new value', () => {
expect(pipe.transform('bar')).toEqual('Bar');
expect(pipe.transform('foo')).toEqual('Foo');
});

it('should not support other objects',
() => { expect(() => pipe.transform(<any>{})).toThrowError(); });
});

describe('UpperCasePipe', () => {
let pipe: UpperCasePipe;

beforeEach(() => { pipe = new UpperCasePipe(); });

it('should return uppercase', () => { expect(pipe.transform('foo')).toEqual('FOO'); });

it('should uppercase when there is a new value', () => {
expect(pipe.transform('foo')).toEqual('FOO');
expect(pipe.transform('bar')).toEqual('BAR');
});

it('should not support other objects',
() => { expect(() => pipe.transform(<any>{})).toThrowError(); });
});
}
42 changes: 0 additions & 42 deletions modules/@angular/common/test/pipes/lowercase_pipe_spec.ts

This file was deleted.

43 changes: 0 additions & 43 deletions modules/@angular/common/test/pipes/uppercase_pipe_spec.ts

This file was deleted.

5 changes: 5 additions & 0 deletions tools/public_api_guard/common/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ export declare class SlicePipe implements PipeTransform {
transform(value: any, start: number, end?: number): any;
}

/** @stable */
export declare class TitleCasePipe implements PipeTransform {
transform(value: string): string;
}

/** @stable */
export declare class UpperCasePipe implements PipeTransform {
transform(value: string): string;
Expand Down

0 comments on commit 61d7c1e

Please sign in to comment.