From 61d7c1e0b3864be45286d7b89972439df0970d91 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 8 Dec 2016 16:33:24 -0800 Subject: [PATCH] feat(common): add a `titlecase` pipe (#13324) closes #11436 --- modules/@angular/common/src/common.ts | 2 +- .../common/src/pipes/case_conversion_pipes.ts | 61 +++++++++++++++++++ modules/@angular/common/src/pipes/index.ts | 6 +- .../common/src/pipes/lowercase_pipe.ts | 37 ----------- .../common/src/pipes/uppercase_pipe.ts | 36 ----------- .../test/pipes/case_conversion_pipes_spec.ts | 59 ++++++++++++++++++ .../common/test/pipes/lowercase_pipe_spec.ts | 42 ------------- .../common/test/pipes/uppercase_pipe_spec.ts | 43 ------------- tools/public_api_guard/common/index.d.ts | 5 ++ 9 files changed, 130 insertions(+), 161 deletions(-) create mode 100644 modules/@angular/common/src/pipes/case_conversion_pipes.ts delete mode 100644 modules/@angular/common/src/pipes/lowercase_pipe.ts delete mode 100644 modules/@angular/common/src/pipes/uppercase_pipe.ts create mode 100644 modules/@angular/common/test/pipes/case_conversion_pipes_spec.ts delete mode 100644 modules/@angular/common/test/pipes/lowercase_pipe_spec.ts delete mode 100644 modules/@angular/common/test/pipes/uppercase_pipe_spec.ts diff --git a/modules/@angular/common/src/common.ts b/modules/@angular/common/src/common.ts index 696834f905..9ca4887ad6 100644 --- a/modules/@angular/common/src/common.ts +++ b/modules/@angular/common/src/common.ts @@ -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'; diff --git a/modules/@angular/common/src/pipes/case_conversion_pipes.ts b/modules/@angular/common/src/pipes/case_conversion_pipes.ts new file mode 100644 index 0000000000..2b2965a5d8 --- /dev/null +++ b/modules/@angular/common/src/pipes/case_conversion_pipes.ts @@ -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(); + } +} diff --git a/modules/@angular/common/src/pipes/index.ts b/modules/@angular/common/src/pipes/index.ts index 26d20ae8f9..7014c6275d 100644 --- a/modules/@angular/common/src/pipes/index.ts +++ b/modules/@angular/common/src/pipes/index.ts @@ -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, @@ -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. */ @@ -46,6 +47,7 @@ export const COMMON_PIPES = [ SlicePipe, DecimalPipe, PercentPipe, + TitleCasePipe, CurrencyPipe, DatePipe, I18nPluralPipe, diff --git a/modules/@angular/common/src/pipes/lowercase_pipe.ts b/modules/@angular/common/src/pipes/lowercase_pipe.ts deleted file mode 100644 index 037c211cf8..0000000000 --- a/modules/@angular/common/src/pipes/lowercase_pipe.ts +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @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 {isBlank} from '../facade/lang'; -import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; - - -/** - * @ngModule CommonModule - * @whatItDoes Transforms string to lowercase. - * @howToUse `expression | lowercase` - * @description - * - * Converts value into a lowercase string using `String.prototype.toLowerCase()`. - * - * ### Example - * - * {@example common/pipes/ts/lowerupper_pipe.ts region='LowerUpperPipe'} - * - * @stable - */ -@Pipe({name: 'lowercase'}) -export class LowerCasePipe implements PipeTransform { - transform(value: string): string { - if (isBlank(value)) return value; - if (typeof value !== 'string') { - throw new InvalidPipeArgumentError(LowerCasePipe, value); - } - return value.toLowerCase(); - } -} diff --git a/modules/@angular/common/src/pipes/uppercase_pipe.ts b/modules/@angular/common/src/pipes/uppercase_pipe.ts deleted file mode 100644 index fdfeceb9c8..0000000000 --- a/modules/@angular/common/src/pipes/uppercase_pipe.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * @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 {isBlank} from '../facade/lang'; -import {InvalidPipeArgumentError} from './invalid_pipe_argument_error'; - -/** - * @ngModule CommonModule - * @whatItDoes Transforms string to uppercase. - * @howToUse `expression | uppercase` - * @description - * - * Converts value into an uppercase string using `String.prototype.toUpperCase()`. - * - * ### Example - * - * {@example common/pipes/ts/lowerupper_pipe.ts region='LowerUpperPipe'} - * - * @stable - */ -@Pipe({name: 'uppercase'}) -export class UpperCasePipe implements PipeTransform { - transform(value: string): string { - if (isBlank(value)) return value; - if (typeof value !== 'string') { - throw new InvalidPipeArgumentError(UpperCasePipe, value); - } - return value.toUpperCase(); - } -} diff --git a/modules/@angular/common/test/pipes/case_conversion_pipes_spec.ts b/modules/@angular/common/test/pipes/case_conversion_pipes_spec.ts new file mode 100644 index 0000000000..6fe87b6f04 --- /dev/null +++ b/modules/@angular/common/test/pipes/case_conversion_pipes_spec.ts @@ -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({})).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({})).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({})).toThrowError(); }); + }); +} diff --git a/modules/@angular/common/test/pipes/lowercase_pipe_spec.ts b/modules/@angular/common/test/pipes/lowercase_pipe_spec.ts deleted file mode 100644 index e31e7fc5d5..0000000000 --- a/modules/@angular/common/test/pipes/lowercase_pipe_spec.ts +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @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} from '@angular/common'; -import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal'; - -export function main() { - describe('LowerCasePipe', () => { - let upper: string; - let lower: string; - let pipe: LowerCasePipe; - - beforeEach(() => { - lower = 'something'; - upper = 'SOMETHING'; - pipe = new LowerCasePipe(); - }); - - describe('transform', () => { - it('should return lowercase', () => { - const val = pipe.transform(upper); - expect(val).toEqual(lower); - }); - - it('should lowercase when there is a new value', () => { - const val = pipe.transform(upper); - expect(val).toEqual(lower); - const val2 = pipe.transform('WAT'); - expect(val2).toEqual('wat'); - }); - - it('should not support other objects', - () => { expect(() => pipe.transform({})).toThrowError(); }); - }); - - }); -} diff --git a/modules/@angular/common/test/pipes/uppercase_pipe_spec.ts b/modules/@angular/common/test/pipes/uppercase_pipe_spec.ts deleted file mode 100644 index 244751bbeb..0000000000 --- a/modules/@angular/common/test/pipes/uppercase_pipe_spec.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @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 {UpperCasePipe} from '@angular/common'; -import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal'; - -export function main() { - describe('UpperCasePipe', () => { - let upper: string; - let lower: string; - let pipe: UpperCasePipe; - - beforeEach(() => { - lower = 'something'; - upper = 'SOMETHING'; - pipe = new UpperCasePipe(); - }); - - describe('transform', () => { - - it('should return uppercase', () => { - const val = pipe.transform(lower); - expect(val).toEqual(upper); - }); - - it('should uppercase when there is a new value', () => { - const val = pipe.transform(lower); - expect(val).toEqual(upper); - const val2 = pipe.transform('wat'); - expect(val2).toEqual('WAT'); - }); - - it('should not support other objects', - () => { expect(() => pipe.transform({})).toThrowError(); }); - }); - - }); -} diff --git a/tools/public_api_guard/common/index.d.ts b/tools/public_api_guard/common/index.d.ts index b6d34bd589..223641ddb2 100644 --- a/tools/public_api_guard/common/index.d.ts +++ b/tools/public_api_guard/common/index.d.ts @@ -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;