-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add a
titlecase
pipe (#13324)
closes #11436
- Loading branch information
Showing
9 changed files
with
130 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
modules/@angular/common/src/pipes/case_conversion_pipes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
modules/@angular/common/test/pipes/case_conversion_pipes_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); }); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters